从php curl post请求获取头信息

sel*_*c82 12 php curl

我一直在寻找几个小时,但在这方面找不到任何东西.我正在对sugarsync api做一个php curl post请求,它会在我需要的标题中返回一个位置.我不知道如何获取这些信息.我必须将它保留为帖子,因为我将xml文件发布到他们的api,他们所做的就是返回标题信息.我不知道如何访问标题中的位置.根据他们我需要将其放入另一个xml文件并发布它.任何帮助表示赞赏.

dre*_*010 15

如果您设置了curl选项CURLOPT_FOLLOWLOCATION,cURL将按照您的位置重定向.

如果要获取标头,请将选项设置CURLOPT_HEADER为1,并且从中返回的HTTP响应curl_exec()将包含标头.你可以解析它们的位置.

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1); // return HTTP headers with response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the response rather than output it

$resp = curl_exec($ch);

list($headers, $response) = explode("\r\n\r\n", $resp, 2);
// $headers now has a string of the HTTP headers
// $response is the body of the HTTP response

$headers = explode("\n", $headers);
foreach($headers as $header) {
    if (stripos($header, 'Location:') !== false) {
        echo "The location header is: '$header'";
    }
}
Run Code Online (Sandbox Code Playgroud)

查看curl_setopt()中的所有选项.


aro*_*ino 2

获取响应中的标头信息。

curlsetopt($ch,CURLOPT_HEADER,true);
Run Code Online (Sandbox Code Playgroud)