V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
rateltalk
V2EX  ›  问与答

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

  •  
  •   rateltalk · Jul 1, 2017 · 1978 views
    This topic created in 3223 days ago, the information mentioned may be changed or developed.

    想要传输数据的格式:

    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 replies    2017-07-01 22:23:59 +08:00
    explon
        1
    explon  
       Jul 1, 2017 via iPhone
    param 是 string, 你理解错误
    lsido
        2
    lsido  
       Jul 1, 2017
    对不起,这个排版让我涌出一股鬼火

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

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

    解决办法是有的:

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

    接收:

    array(1) {
    ["param"]=>
    array(1) {
    ["returnUrl"]=>
    string(9) "returnUrl"
    }
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3483 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 12:05 · PVG 20:05 · LAX 05:05 · JFK 08:05
    ♥ Do have faith in what you're doing.