POST cURL请求REST API发送

时间:2012-03-23 10:44:59

标签: php curl

我已经尝试了一段时间来调用REST API,但仍然没有运气。

我已经使用WizTools测试了连接,propreties和凭据,因此我100%所有数据都是正确且有效的。只有当我尝试使用自定义PHP连接到API时,事情才会出错。

我使用Fiddler进行调试,我看到的是使用代码请求文件的本地路径。出于某种原因,该片段没有调用REST端点...我想知道它是如何出现的,我做错了什么......

代码是本地托管的(dev.testing.com) 乍一看,我认为下面的代码是正确的,所以我想知道为什么我在Fiddler中得到GET http://dev.testing.com/talent.php HTTP/1.1结果,而我应该得到POST https://api.some_url.com/package/REST/service/criteria?api_key=my_Key HTTP/1.1 ..

<?php
       // Set all the data
       $service_url = "https://api.some_url.com/package/REST/service/criteria?api_key=my_Key";
       $service_user = 'iiii_My_Username:text:FO';
       $service_pass = 'password';

       // Initialize the cURL
       $ch = curl_init($service_url);

       // Set service authentication
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
       curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");

       // Composing the HTTP headers     
       $body = "<searchCriteriaSorting></searchCriteriaSorting>";

       $headers = array();
       $headers[] = 'Accept: application/xml';
       $headers[] = 'Content-Type: application/xml; charset=UTF-8';

       // Set the cURL options
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
       curl_setopt($ch, CURLOPT_POST, TRUE);
       curl_setopt($ch, CURLOPT_VERBOSE, 1);
       curl_setopt($ch, CURLOPT_HEADER, TRUE);
       curl_setopt($ch, CURLINFO_HEADER_OUT, true);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       curl_setopt($ch, CURLOPT_ENCODING, '');
       curl_setopt($ch, CURLOPT_TIMEOUT, 15);

       //WARNING: this would prevent curl from detecting a 'man in the middle' attack
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

       // Execute the cURL
       $data = curl_exec($ch);

       // Print the result
       echo $data;
?>

0 个答案:

没有答案