从 php curl post 请求中获取头信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11659460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:48:49  来源:igfitidea点击:

get header information from php curl post request

phpcurl

提问by selanac82

I've been searching for hours and can't find anything on this. I'm doing a php curl post request to the sugarsync api and it returns a location in the headers that i need. i have no idea how to get this information. i have to keep it as a post because i post an xml file to their api and all they do is return header information. i have no clue how i can access the location in the headers. according to themi need to put it into another xml file and post that as well. any help is appreciated.

我已经搜索了几个小时,但找不到任何关于此的信息。我正在向 Sugarsync api 执行 php curl post 请求,它返回我需要的标头中的位置。我不知道如何获得这些信息。我必须将其保留为帖子,因为我将 xml 文件发布到他们的 api,而他们所做的只是返回标题信息。我不知道如何访问标题中的位置。根据他们的说法,我需要将其放入另一个 xml 文件并发布。任何帮助表示赞赏。

回答by drew010

If you set the curl option CURLOPT_FOLLOWLOCATION, cURL will follow the location redirect for you.

如果你设置了 curl 选项CURLOPT_FOLLOWLOCATION,cURL 会为你跟随位置重定向。

If you want to get the headers, set the option CURLOPT_HEADERto 1, and the HTTP response you get back from curl_exec()will contain the headers. You can them parse them for the location.

如果您想获取标头,请将选项设置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'";
    }
}

See all of the options at curl_setopt().

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

回答by arosolino

To get the header information in the response.

获取响应中的标头信息。

curlsetopt($ch,CURLOPT_HEADER,true);