PHP CURL正在显示"您请求的页面无效"而它与邮递员合作

时间:2017-05-02 07:08:44

标签: php curl postman

我正在调用与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);

2 个答案:

答案 0 :(得分:0)

我会说https,添加:

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

答案 1 :(得分:0)

您也可以从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;
 }