[乐意黎原创]php curl 分离header和body信息

小黎在进行curl 抓取时,会返回如下所列信息。

这时,我并不需要表头信息,只需要json数据。

返回信息: 

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache, must-revalidate
Connection: keep-alive
Content-Length: 37220


{"ret":0,"errmsg":"ok","general_msg_list":"{\"list\":[{\"app_msg_ext_info\":{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=1&sn=e1e2e2696baf49eef6ebc9fc23b00e99&chksm=bd3aeaff8a4d63e9ad70f770d708044f9137a2ade9476eb84020627a78436650d00062cded90&scene=4#wechat_redirect\",\"copyright_stat\":11,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXaDc5E5Gp3S2DicFUZXcwE4lJ6IJVpibXzO47umFCDZVUBsRS8eibAAW6g\/0?wx_fmt=jpeg\",\"digest\":\"-▼-女神美如花,等你么么哒!点击公众号菜单【变变直播】别磨蹭,快戳进来!点击下方【阅读原文】有直达车哦!\",\"fileid\":504667479,\"is_multi\":1,\"multi_app_msg_item_list\":[{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=2&sn=7d3c688407e88ec3e72416ddeb244d79&chksm=bd3aeaff8a4d63e900f78c6216d8480c5d32d8e43c521b6341576dfb30eaf698908ddec460a7&scene=4#wechat_redirect\",\"copyright_stat\":100,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXPiajwlsnb9KQrnUFvVRXvQUunriaRA54ZMuAXKZKFnSxnDk6EPn7zofg\/0?wx_fmt=jpeg\",\"digest\":\"变变寄语:楼下超市新来了个收银小妹,为了撩她,变变每天都回去几次。为了显得自己有钱,变变每次都会拿一百块的钱\",\"fileid\":504667482,\"source_url\":\"http:\/\/i.youku.com\/u\/UMTA3OTczNTEy\",\"title\":\"奇葩聊 | 独在异乡为异客,活的心累嘴也累!\"},{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=3&sn=a5d869242f23c36520778f0f8e3d703f&chksm=bd3aeaff8a4d63e926eaa648e7e99260094f4d23ba78e145d1583d864a2b7bb2ae3e34daf52c&scene=4#wechat_redirect\",\"copyright_stat\":100,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXycWEQFdicNblBH45iaeF56uk5UkEHwmJGetriafo0fV4cFicq5tKhB63sg\/0?wx_fmt=jpeg\",\"digest\":\"变变寄语:变变刚毕业工作那会,还是临时工。有次公司加班,变变无聊就嗑瓜子,这是老板怒斥变变:“都在加班,你怎\",\"fileid\":504667484,\"source_url\":\"http:\/\/i.youku.com\/u\/UMTA3OTczNTEy\",\"title\":\"变变格漫 | 悲痛涌上心头,男人成了冤大头!\"} ....]}

设置的参数如下:

curl_setopt($ch, CURLOPT_HEADER, FALSE);    //TRUE: 需要response header, FALSE: 不返回 header
curl_setopt($ch, CURLOPT_NOBODY, FALSE);   //表示需要response body

当设置

curl_setopt($ch, CURLOPT_HEADER, FALSE); 


则不返回表头了. 

只有

{"ret":0,"errmsg":"ok","general_msg_list":"{\"list\":[{\"app_msg_ext_info\":{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=1&sn=e1e2e2696baf49eef6ebc9fc23b00e99&chksm=bd3aeaff8a4d63e9ad70f770d708044f9137a2ade9476eb84020627a78436650d00062cded90&scene=4#wechat_redirect\",\"copyright_stat\":11,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXaDc5E5Gp3S2DicFUZXcwE4lJ6IJVpibXzO47umFCDZVUBsRS8eibAAW6g\/0?wx_fmt=jpeg\",\"digest\":\"-▼-女神美如花,等你么么哒!点击公众号菜单【变变直播】别磨蹭,快戳进来!点击下方【阅读原文】有直达车哦!\",\"fileid\":504667479,\"is_multi\":1,\"multi_app_msg_item_list\":[{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=2&sn=7d3c688407e88ec3e72416ddeb244d79&chksm=bd3aeaff8a4d63e900f78c6216d8480c5d32d8e43c521b6341576dfb30eaf698908ddec460a7&scene=4#wechat_redirect\",\"copyright_stat\":100,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXPiajwlsnb9KQrnUFvVRXvQUunriaRA54ZMuAXKZKFnSxnDk6EPn7zofg\/0?wx_fmt=jpeg\",\"digest\":\"变变寄语:楼下超市新来了个收银小妹,为了撩她,变变每天都回去几次。为了显得自己有钱,变变每次都会拿一百块的钱\",\"fileid\":504667482,\"source_url\":\"http:\/\/i.youku.com\/u\/UMTA3OTczNTEy\",\"title\":\"奇葩聊 | 独在异乡为异客,活的心累嘴也累!\"},{\"author\":\"陈翔\",\"content\":\"\",\"content_url\":\"http:\/\/mp.weixin.qq.com\/s?__biz=MjM5NzM4MDg1MA==&mid=2652151138&idx=3&sn=a5d869242f23c36520778f0f8e3d703f&chksm=bd3aeaff8a4d63e926eaa648e7e99260094f4d23ba78e145d1583d864a2b7bb2ae3e34daf52c&scene=4#wechat_redirect\",\"copyright_stat\":100,\"cover\":\"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/jS2QdfS7ggKCicdZeBXyVHgMz68KauvdXycWEQFdicNblBH45iaeF56uk5UkEHwmJGetriafo0fV4cFicq5tKhB63sg\/0?wx_fmt=jpeg\",\"digest\":\"变变寄语:变变刚毕业工作那会,还是临时工。有次公司加班,变变无聊就嗑瓜子,这是老板怒斥变变:“都在加班,你怎\",\"fileid\":504667484,\"source_url\":\"http:\/\/i.youku.com\/u\/UMTA3OTczNTEy\",\"title\":\"变变格漫 | 悲痛涌上心头,男人成了冤大头!\"} ....]}


查询了下,一是通过curl自带的curl_getinfo()方法获取头的长度,然后使用substr来分割字符串。示例代码如下:

$response = curl_exec($ch);

if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $headerSize);
    $body = substr($response, $headerSize);
}

第二种方法基于header和body是通过两个回车换行来分割的,所以可以通过如下代码实现:

$response = curl_exec($ch);

if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
    list($header, $body) = explode("\r\n\r\n", response, 2);
}


大功造成。

mark 一下.


本文地址: http://blog.csdn.net/aerchi/article/details/55099530

发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

如何使用php curl使用multipart / form-data body创建HTTP POST请求

02-14

<div class="post-text" itemprop="text"> <p>I need to generate a HTTP post request accordingly to documentation <a href="http://service.ringcentral.com/faxoutapi/" rel="nofollow">found here</a>.</p> <p>Here is an example of how the request body should look like</p> <pre><code>Content-Type: multipart/form-data; boundary=---------------------------7d54b1fee05aa -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Username" 5556090455 -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Password" qwerty -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Attachment"; filename="C:\example.doc" <Document content is here> -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Recipient" 5556465589|John Doe -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Recipient" 5555568552|John Smith -----------------------------7d54b1fee05aa Content-Disposition: form-data; name="Coverpagetext" This is a test fax from web -----------------------------7d54b1fee05aa-- </code></pre> <p>Here is the PHP code that I have so far</p> <pre><code>$postURL = 'https://service.ringcentral.com/faxapi.asp'; $ch = curl_init($postURL); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_MAXREDIRS, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com/'); $hiddens = ''; $hiddens .= 'Username'.'='.'8558415124'.'&'; $hiddens .= 'Password'.'='.'Champion92'.'&'; $hiddens .= 'Recipient'.'='.'8882466583'.'&'; $hiddens .= 'Coverpage'.'='.'NONE'.'&'; $hiddens .= 'Coverpagetext'.'='.'asdf asdf'.'&'; $hiddens .= 'Attachment'.'='.'C:\example.doc'.'&'; $hiddens = substr($hiddens, 0, strlen($hiddens)-1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $hiddens); $page = curl_exec($ch); echo $page;// output result if ($page === FALSE) { var_dump(curl_getinfo($ch)); exit( "Post: FAILED = ".curl_error($ch) ); } curl_close($ch); // close the connection </code></pre> <p>I am getting a response of 1 (which means Authorization failed). I have the feeling that I am doing a normal post request and the authorization fails because the request is not well formatted.</p> <p>How can I turn this as Multipart request? And how can I troubleshoot the HTTP header I am sending, not the one I receive?</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览