dongzhoulong1797 2011-06-29 12:16
浏览 76
已采纳

cUrl,PHP和访问特定标头类型

I am writing a test harness to assist in testing a method in a PHP class that I have. Essentially, that method sends XML data as a POST request to the LinkedIn API, so for testing purposes, my test harness will act as the mock LinkedIn API endpoint, taking the posted data and returning various responses to see how the method handles them.

So, the method uses cUrl to send data like so:

$handle = curl_init()

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, 'http://localhost/target.php');
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);

$header[] = 'Content-Type: text/xml; charset=UTF-8';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);

$data = '<?xml version="1.0" encoding="UTF-8"?>
         <activity locale="en_US">
           <content-type>linkedin-html</content-type>
           <body>Network update</body>
         </activity>';
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($handle);

Now, on my target.php script, I'd like to access the POSTed data... but when sending non key=value paired data, $_POST is always empty. If I omit the custom header and replace $data with $data = 'key=value', $_POST['key'] contains value. Keep in mind that this method works in terms of sending the XML data as above and the LinkedIn API responding.

So, any pointers on how to access POSTed data that is sent in non-key value pair format and possible a custom header type?

  • 写回答

2条回答 默认 最新

  • doukang1962 2011-06-30 13:31
    关注

    Looks like the correct solution is to access the raw POST data via:

    <?php $postdata = file_get_contents("php://input"); ?>
    

    Per http://php.net/manual/en/reserved.variables.httprawpostdata.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?