从php curl post请求获取头信息 [英] get header information from php curl post request

查看:582
本文介绍了从php curl post请求获取头信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索小时,并且找不到任何东西。我正在做一个php curl post请求到sugarsync api,它返回我需要的标题中的位置。我不知道如何获得这些信息。我必须保持它作为一个职位,因为我发布一个XML文件到他们的api,他们做的是返回头信息。我没有线索如何我可以访问标题中的位置。 根据他们我需要把它放到另一个XML文件,并发布,以及。任何帮助是赞赏。

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 them i need to put it into another xml file and post that as well. any help is appreciated.

推荐答案

如果您设置curl选项 CURLOPT_FOLLOWLOCATION ,cURL将跟随

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

如果您想要获取标题,请将选项 CURLOPT_HEADER 设置为1,从 curl_exec()返回的HTTP响应将包含标头。您可以为他们解析位置。

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

$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'";
    }
}

查看 curl_setopt()

这篇关于从php curl post请求获取头信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆