【问题标题】:PHP CURL is showing "The page you requested is invalid" while it is working with PostmanPHP CURL 在使用 Postman 时显示“您请求的页面无效”
【发布时间】:2017-05-02 15:31:47
【问题描述】:

我正在调用与 Postman 一起正常工作的 API,但显示 PHP CURL“无效请求”的错误。我使用了以下代码。

 $postData = array(
 'oauth_consumer_key' => 'XXXXXXXXXXXXXXXXXXXXXX',
 'oauth_token' =>'',
 'oauth_signature_method' => 'PLAINTEXT',
 'oauth_timestamp' =>time(),
 'oauth_nonce' =>'DQp8tIsd',
 'oauth_version' =>'1.0',
'oauth_signature' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
 );

$url = 'https://api.sandboxcernercare.com/oauth/access'; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-
form-urlencoded'));   
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);
echo "<pre>";
print_r($result);

【问题讨论】:

    标签: php curl postman


    【解决方案1】:

    我会说 https,添加:

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    

    【讨论】:

      【解决方案2】:

      您也可以从 Postman 生成 PHP CURL 代码。它应该如下所示。

       $curl = curl_init();
       curl_setopt_array($curl, array(
       CURLOPT_URL => "https://api.sandboxcernercare.com/oauth/access",
       CURLOPT_RETURNTRANSFER => true,
       CURLOPT_ENCODING => "",
       CURLOPT_MAXREDIRS => 10,
       CURLOPT_TIMEOUT => 30,
       CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
       CURLOPT_CUSTOMREQUEST => "POST",
       CURLOPT_HTTPHEADER => array(
        "authorization: OAuth oauth_consumer_key=\"XXXXXXXXXXXXXXXXX\",oauth_signature_method=\"PLAINTEXT\",oauth_timestamp=\"1493709577\",oauth_nonce=\"O9xz0w\",oauth_version=\"1.0\",oauth_signature=\"XXXXXXXXXXXXXXXXXXXXXXXXX\"",
      "cache-control: no-cache",
      "content-type: application/x-www-form-urlencoded",
      "postman-token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
       ),
      ));
      
       $response = curl_exec($curl);
       $err = curl_error($curl);
      
       curl_close($curl);
      
       if ($err) {
        echo "cURL Error #:" . $err;
       } else {
      
         echo $response;
       }
      

      【讨论】:

        猜你喜欢
        • 2018-10-27
        • 1970-01-01
        • 2010-11-23
        • 1970-01-01
        • 2017-06-17
        • 2017-01-08
        • 1970-01-01
        • 2011-11-16
        • 2016-08-12
        相关资源
        最近更新 更多