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

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
$get_data = array (
    "get1"=> "get1",
    "get2" => "get2",
    "get3" => "get3"
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://test.test.com/test.php?'.http_build_query($get_data));
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');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$post_data = array (
    "p1" => "test1",
    "p2" => "test2",
    "p3" => "test3"
);
curl_setopt($curl, CURLOPT_POST, true);
//["CONTENT_TYPE"]=> string(70) "multipart/form-data; boundary=------077a996f5afe"
//要发送文件,在文件名前面加上@前缀并使用完整路径。
//使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,默认把content_type设为了multipart/form-data。
//虽然对于大多数web服务器并没有影响,但是还是有少部分服务器不兼容。
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
//["CONTENT_TYPE"]=> string(33) "application/x-www-form-urlencoded"
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
//在没有需要上传文件的情况下,尽量对post提交的数据进行http_build_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;

相关文章

php自定义错误处理用法实例

本文实例讲述了php自定义错误处理用法。分享给大家供大家参考。具体如下: <?php error_reporting(E_ALL); function ErrHandl...

全面解析PHP验证码的实现原理 附php验证码小案例

拓展 我们需要开启gd拓展,可以使用下面的代码来查看是否开启gd拓展。 <?php echo "Hello World!!!!"; echo phpinfo();...

PHP的serialize序列化数据以及JSON格式化数据分析

PHP的serialize是将变量序列化,返回一个具有变量类型和结构的字符串表达式,而JSON则是一种更轻、更友好的用于接口(AJAX、REST等)数据交换的格式。 其实两者都是以一种字...

php获取今日开始时间和结束时间的方法

话不多说,请看代码:  $begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')))...

PHP Array交叉表实现代码

如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码 复制代码 代码如下: /** * 基本交叉表 * @author hugh * */ cl...