• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

解析curl提交GET,POST,Cookie的简单方法_php技巧

php 搞代码 4年前 (2022-01-26) 16次浏览 已收录 0个评论
<?php<BR>$get_data = array (<BR>    "get1"=> "get1",<BR>    "get2" => "get2",<BR>    "get3" => "get3"<BR>);<BR>$curl = curl_init();<BR>curl_setopt($curl, CURLOPT_URL, 'http://test.test.com/test.php?'.http_build_query($get_data));<BR>curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11');<BR>curl_setopt($curl, CURLOPT_HEADER, false);<BR>curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);<BR>curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);<BR>$post_data = array (<BR>    "p1" => "test1",<BR>    "p2" => "test2",<BR>    "p3" => "test3"<BR>);<BR>curl_setopt($curl, CURLOPT_POST, true);<BR>//["CONTENT_TYPE"]=> string(70) "multipart/form-data; boundary=------077a996f5afe"<BR>//要发送文件,在文件名前面加上@前缀并使用完整路径。<BR>//使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,默认把content_type设为了multipart/form-data。<BR>//虽然对于大多数web服务器并没有影响,但是还是有少部分服务器不兼容。<BR>curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);<BR>//["CONTENT_TYPE"]=> string(33) "application/x-www-form-urlencoded"<BR>//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));<BR>//在没有需要上传文件的情况下,尽量对post提交的数据进行http_b<div style="color:transparent">本文来源gaodai.ma#com搞##代!^码@网*</div><pre>搞gaodaima代码

uild_query,然后发送出去,能实现更好的兼容性,更小的请求数据包。
$cookies = array(
‘c1’=>’v1’,
‘c2’=>’v2’,
‘c3’=>’v3’,
);
$cookies_string = ”;
foreach($cookies as $name=>$value){
$cookies_string .= $name.’=’.$value.’;’;
}
curl_setopt($curl, CURLOPT_COOKIE, $cookies_string);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
exit;


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解析curl提交GET,POST,Cookie的简单方法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址