V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
s609926202
V2EX  ›  问与答

使用 PHP curl 调用 API,如何传递如下格式的 POSTFIELD?

  •  
  •   s609926202 · 2017-07-01 17:39:30 +08:00 · 1575 次点击
    这是一个创建于 2515 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想要传输数据的格式:

    param = { 'xx' : 'ss', 'xx' : 'ss', 'xx' : 'ss', } 我自己写的:

    $params = array( 'param' => array( 'orderSn' => $info['ordersn'], 'returnUrl' => self::RETURN_URL, 'notifyUrl' => self::NOTIFY_URL, 'cancelUrl' => self::CANCEL_URL, 'productInfo' => $info['productname'], 'productCount' => 1, 'productPrice' => $info['total_fee'], 'productDiscountPrice' => '0', 'productPriceCurrency' => 'CNY' ) );

    // 获取 Paypal 支付跳转地址 $timout = 300; $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, self::REQUIRE_URL ); curl_setopt( $ch, CURLOPT_POST, TRUE ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $params ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timout ); 求解!

    2 条回复    2017-07-01 22:23:59 +08:00
    explon
        1
    explon  
       2017-07-01 17:45:31 +08:00 via iPhone
    param 是 string, 你理解错误
    lsido
        2
    lsido  
       2017-07-01 22:23:59 +08:00
    对不起,这个排版让我涌出一股鬼火

    你的意思是 PHP 用 curl post 一个数组

    直接提交是不行的,毕竟 POST 是 Key-Value

    解决办法是有的:

    用 http_build_query 处理你的$params 提交即可

    接收:

    array(1) {
    ["param"]=>
    array(1) {
    ["returnUrl"]=>
    string(9) "returnUrl"
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1156 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.