cURL POST方法不起作用

时间:2014-07-18 04:44:29

标签: php curl yii

我正在使用cURL来调用REST API。以下代码适用于 GET方法,但是对于 POST方法,在添加后调用它并不起作用:

        curl_setopt($ch, CURLOPT_POST, true);

可能是什么原因?

$options = array(
                CURLOPT_RETURNTRANSFER => true,     // return web page
                CURLOPT_HEADER         => false,    // don't return headers
                CURLOPT_FOLLOWLOCATION => true,     // follow redirects
                CURLOPT_ENCODING       => "",       // handle all encodings
                CURLOPT_USERAGENT      => "spider", // who am i
                CURLOPT_AUTOREFERER    => true,     // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
                CURLOPT_TIMEOUT        => 120,      // timeout on response
                CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
                CURLOPT_SSL_VERIFYPEER => false,    // Disabled SSL Cert checks
                CURLOPT_HTTPHEADER     => array('appId: C579929D-F0AA-4A8F-B6C6-1FF96694483C','appKey: AE78E7F1-FBDE-45F5-BAC2-210CEE9D3ED9')
               );

            $url = "some-url";
            $ch = curl_init($url);
            curl_setopt_array($ch, $options);
            curl_setopt($ch, CURLOPT_POST, true);
            $output = curl_exec($ch);
            echo $output;

P.S

我使用PostMan仔细检查了API调用,并返回了预期的结果。

我是否需要为API调用添加其他属性?

我必须调用格式的REST API:

https://api.abc.com/v1/transactions/companyid/123/userid/456/description/ppeck/‌​source/PPA

正如答案所示,我已将代码更新为:

        $url = "https://api.abc.com/v1/transactions";
        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "companyid=12301&userid=31401&description=ppa-check&source=PPA&amount=123&txndate=010111&txntypeid=420&customerid=2701");
        $output = curl_exec($ch);

遗憾的是它没有用。

1 个答案:

答案 0 :(得分:2)

试 帖子字段必须是一个追加(&)的查询字符串,如

    $url = 'http://example.com';  //your api call url
    $fields = 'q=example&y=fgt&ghty'; // all posting fields separated with &
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
相关问题