如何在php中获取curl请求的请求头信息及相应头信息

如何在php中获取curl请求的请求头信息及相应头信息,第1张

oCurl = curl_init()

// 设置请求

$header[] = "Content-type: application/x-www-form-urlencoded"

$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"

curl_setopt($oCurl, CURLOPT_URL, $sUrl)

curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header)

// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文

curl_setopt($oCurl, CURLOPT_HEADER, true)

// 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文

curl_setopt($oCurl, CURLOPT_NOBODY, true)

// 使用上面定义的 ua

curl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent)

curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 )

// 不用 POST 方式请求, 意思就是通过 GET 请求

curl_setopt($oCurl, CURLOPT_POST, false)

$sContent = curl_exec($oCurl)

// 获得响应结果里的:头大小

$headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE)

// 根据头大小去获取头信息内容

$header = substr($sContent, 0, $headerSize)

    

curl_close($oCurl)

其实curl里面早就有对HEAD协议的支持// 只需要在你的代码中加上这样一行,就会自动选择head协议

curl_setopt($ch, CURLOPT_NOBODY, true)

如果你要读取

Content-Length

,那么只需要在curl_exec后// 读取的header里的Content-Length值

$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD)

需要说明的是HEAD协议虽然被大部分服务器支持,但也不是说所有的服务器都支持,有的服务器为了防抓取,在设置中干掉了这个协议。而

Content-Length

也不是必须的字段,你应该做到如果有这个值,而且超过了最大值,可以返回错误,如果没有这个值,或者没有超过最大值,就必须自己通过已经下载的内容大小来判断。

至于你说的最大资源下载长度,我还没看到这个设置项,不过这个问题有一个更加美好的解决方案,那就是用到

CURLOPT_HEADERFUNCTION和CURLOPT_WRITEFUNCTION

两个回调,那么就只需要一次请求即可完成所有的判断,而且可以随时断掉$size = 0$max_size = 123456

curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $str) {

// 第一个参数是curl资源,第二个参数是每一行独立的header!

list ($name, $value) = array_map('trim', explode(':', $str, 2))

$name = strtolower($name)

// 判断大小啦

if ('content-length' == $name) {

if ($value $max_size) {

return 0// 返回0就会中断读取}}})

// 对于没有content-length的,我们一边读取一边判断

curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $str) use (&$size) {

$len = strlen($str)

$size += $len

if ($size $max_size) {

本文实例讲述了php使用CURL模拟GET与POST向微信接口提交及获取数据的方法。分享给大家供大家参考,具体如下:

php CURL函数可以模仿用户进行一些 *** 作,如我们可以模仿用户提交数据也可以模仿用户进行网站访问了,下面我们来介绍利用CURL模拟进行微信接口的GET与POST例子,例子非常的简单就两个:

Get提交获取数据

/**

* @desc 获取access_token

* @return String access_token

*/

function getAccessToken(){

$AppId = '1232assad13213123'

$AppSecret = '2312312321adss3123213'

$getUrl = 'htq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret

$ch = curl_init()

curl_setopt($ch, CURLOPT_URL, $getUrl)

curl_setopt($ch, CURLOPT_HEADER, 0)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)

curl_setopt($ch, CURL_SSLVERSION_SSL, 2)

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE)

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE)

$data = curl_exec($ch)

$response = json_decode($data)

return $response->access_token

}

post提交获取数据

/**

* @desc 实现天气内容回复

*/

public function testWeixin(){

$access_token = $this->getAccessToken()

$customMessageSendUrl = 'ht.qq.com/cgi-bin/message/custom/send?access_token='.$access_token

$description = '今天天气的详细信息(从第三方获取)。'

$url = ttpr.com/'

$picurl = 'her.com/'

$postDataArr = array(

'touser'=>'OPENID',

'msgtype'=>'news',

'news'=>array(

'articles'=>array(

'title'=>'当天天气',

'description'=>$description,

'url'=>$url,

'picurl'=>$picurl,

),

),

)

$postJosnData = json_encode($postDataArr)

$ch = curl_init($customMessageSendUrl)

curl_setopt($ch, CURLOPT_HEADER, 0)

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)

curl_setopt($ch, CURLOPT_POST, 1)

curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData)

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE)

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE)

$data = curl_exec($ch)

var_dump($data)

}

例子相对来说比较简单也没有什么好详细分析的了,大家照抄就可以实现我们想要的功能了.


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11729393.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存