注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

Getting response header in PHP cURL request  

2010-07-26 16:51:22|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源:http://www.sitepoint.com/forums/showthread.php?t=590248

Dec 20, 2008, 03:06   #1
SitePoint Addict
 
ArunB's Avatar
 
Join Date: Jun 2008
Location: Hyderabad
Posts: 250
Getting response header in PHP cURL request

In my PHP page, I am sending a cURL request to another page. I am getting the response as:

XML Code:
HTTP/1.1 200 OK
Date: Sat, 20 Dec 2008 10:54:25 GMT
Content-Length: 62
Content-Type: text/plain
Cache-control: private
 
<?xml version="1.0"?>
<A>
    <B>SitePoint</B>
    <C>Forum</C>
</A>

How can I get only the header part of this response? i.e.,

XML Code:
HTTP/1.1 200 OK
Date: Sat, 20 Dec 2008 10:54:25 GMT
Content-Length: 62
Content-Type: text/plain
Cache-control: private

Actual requirement is to get the header part and the response string i.e., HTTP/1.1 200 OK. I am assuming that the first line in the response(header) will always contain the response string. Is my assumption is correct?
ArunB is offline   Reply With Quote

Old Dec 20, 2008, 03:15   #2
Programming Team Member
bronze trophy
 
Jake Arkinstall's Avatar
 
Join Date: May 2006
Location: Powys, Mid Wales
Posts: 6,734
You can use curl_getinfo to return the http code:
PHP Code:
 <?php 
$ch
= curl_init('http://yoururl/');
curl_setopt($ch, CURLOPT_HEADER, 1);
$c = curl_exec($ch);
echo
curl_getinfo($ch, CURLINFO_HTTP_CODE);
__________________
Jake || Rest in peace Dan Schulz

< Need a Custom PHP Script? - PM me >
Jake Arkinstall is offline   Reply With Quote
Old Dec 20, 2008, 04:34   #3
SitePoint Enthusiast
 
Join Date: Sep 2008
Posts: 62
To get only the headers use CURLOPT_NOBODY. And yes, the status line is sent before the header fields (by specification).
registrant is offline   Reply With Quote
Old Dec 20, 2008, 07:43   #4
SitePoint Addict
 
ArunB's Avatar
 
Join Date: Jun 2008
Location: Hyderabad
Posts: 250
Hi Guys, Thanks for the quick response.

@arkinstall

If I use this, I will get only Response code(200) only. But I need complete response status(HTTP/1.1 200 Ok).

@registrant

If I have to use this, I need to send two requests, one is for getting headers and one is for getting body content. I don't think that I can do this when working with a page that updates database.



Is there anyway to get header length so that I can get that part from the response.
ArunB is offline   Reply With Quote
Old Dec 20, 2008, 09:18   #5
SitePoint Enthusiast
 
Join Date: Sep 2008
Posts: 62
I misread your first post, thinking you wanted to retrieve the headers only, but I see you were asking how to extract them from the response.

You can explode the response at the header/content boundary:
PHP Code:
  
list($header, $body) = explode("\r\n\r\n", $response, 2);
Or you can use curl_getinfo():
PHP Code:
  
// ...[curl code]...
$info = curl_getinfo($ch);
curl_close($ch);
$header = substr($response, 0, $info['header_size']);
$body = substr($response, -$info['download_content_length']);
Edit: Then to get the status line:
PHP Code:
 $status = strtok($header, "\r\n"); 
registrant is offline   Reply With Quote
Old Dec 22, 2008, 23:12   #6
SitePoint Addict
 
ArunB's Avatar
 
Join Date: Jun 2008
Location: Hyderabad
Posts: 250
It is working. But, sometimes, the value of download_content_length is 0.

And for some POST and PUT requests I am getting HTTP/1.1 100 Continue header first and then getting the POST request headers and body. Is there any way that we can avoid this HTTP/1.1 100 Continue in the cURL response?
ArunB is offline  
  评论这张
 
阅读(1815)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017