如何将此cURL调用转换为PHP?

时间:2015-04-26 17:10:47

标签: php curl php-curl

如何在PHP中表达等效内容?

curl -v -H "Content-Type:application/xml" -H "App-Key:APP_KEY" -X POST "https://api.address" -d '<credentials><partnerId>PARTNER_ID</partnerId><partnerSecret>PARTNER_SECRET</partnerSecret></credentials>'

我所拥有的是以下内容,但无效。

<?php
$ch = curl_init('https://api.address');

curl_setopt($ch, CURLINFO_HEADER_OUT, 1);   
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml', 'App-Key:APP_KEY'));     

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('<credentials><partnerId>PARTNER_ID</partnerId><partnerSecret>PARTNER_SECRET</partnerSecret></credentials>'));                   

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

$response = curl_exec($ch);

?>

我得到的错误是无法连接到主机&#39;。感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

这个怎么样?

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.address');

另请注意,该网址为https,请尝试以下操作:

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