php中使用curl发送请求

curl支持多种协议:http、https、ftp、telnet等,可以根据url前缀是http或https自动选择是否加密发送。


1、以get方式发送查询字符串


//初始化curl

$curl=curl_init();


//设置curl选项

curl_setopt($curl,CURLOPT_URL,"http://www.webzone.cc?查询字符串");  

curl_setopt($curl,CURLOPT_HTTPGET,1);  //默认method为get,method被修改时才使用该选项

//curl_setopt($curl,CURLOPT_RETURNTRANSFER,0);

//curl_setopt($curl,CURLOPT_HEADER,0);


//执行并输出html内容

$output=curl_exec($curl);

//释放curl

curl_close($curl);


说明:

(1)选项CURLOPT_RETURNTRANSFER为设置curl_exec()是否为返回,值0为输出并返回true或false,值1为返回,缺省为0。

(2)选项CURLOPT_HEADER为设置是否返回http头,值0为不返回,值1为返回,缺省为0。

(3)获取curl请求的输出信息:

$info=curl_getinfo($curl);

var_dump($info);


2、以post方式发送查询字符串


$curl=curl_init();

curl_setopt($curl,CURLOPT_URL,"http://www.webzone.cc");  

curl_setopt($curl,CURLOPT_POST,1);  //设置为post方式

curl_setopt($curl,CURLOPT_POSTFIELDS,"查询字符串"); 

/*设置发送的数据,可以是查询字符串、json字符串、xml字符串或数组。

查询字符串、json字符串和xml字符串的编码器类型为application/x-www-form-urlencoded,

数组的编码器类型为multipart/form-data。

函数build_http_query()可将数组转换为查询字符串。

*/

$output=curl_exec($curl);

curl_close($curl);


3、发送json字符串


$curl=curl_init();

curl_setopt($curl,CURLOPT_URL,"http://www.webzone.cc/json.php");

curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,'{"name":"张三","age",18}');  //json字符串外面必须是单引号,还可用json_encode(array('name'=>'张三','age'=>18))

$output=curl_exec($curl);

curl_close($curl);


服务器端json.php的主要内容:

$data=file_get_contents('php://input');

$json=json_decode($data,true);

echo $json['name'];   //显示张三


4、发送xml字符串


$curl=curl_init();

curl_setopt($curl,CURLOPT_URL,"http://www.webzone.cc/xml.php");

curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,
"<?xml version='1.0' encoding='utf-8' ?><group><name>张三</name><age>22</age></group>");

$output=curl_exec($curl);

curl_close($curl);


服务器端xml.php的主要内容:

$data=file_get_contents('php://input');

libxml_disable_entity_loader(true);  //禁止加载外部实体,防止xml实体注入攻击

$xml=simplexml_load_string($data);

//或使用$xml=simplexml_load_string($data,'SimpleXMLElement',LIBXML_NOCDATA);

echo $xml->name;   //显示张三


5、发送数组


$curl=curl_init();

if(PHP_VERSION>5.5){

$data=array('file1'=>new CURLFile(realpath('upload.txt')),'myname'=>'wswh');

}else{

$data=array('file1'=>'@'.realpath('upload.txt'),'myname'=>'wswh');

}

curl_setopt($curl,CURLOPT_URL,"http://www.webzone.cc/upload.php");

curl_setopt($curl,CURLOPT_POST,1);

curl_setopt($curl,CURLOPT_POSTFIELDS,$data);

curl_setopt($curl,CURLOPT_TIMEOUT,100);  //设置超时时间(s)

//curl_setopt($curl,CURLOPT_TIMEOUT_MS,6000);  //设置超时时间(ms)

$output=curl_exec($curl);

curl_close($curl);


服务器端upload.php的主要内容:

if(is_uploaded_file($_FILES['file1']['tmp_name'])){

move_uploaded_file($_FILES['file1']['tmp_name'],realpath("./upload").DIRECTORY_SEPARATOR.basename($_FILES['file1']['tmp_name']));   
}


6、发送https请求


$curl=curl_init();

curl_setopt($curl,CURLOPT_URL,"https://www.webzone.cc");  

curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);  //跳过证书检查

//curl_setopt($curl,CURLOPT_CAINFO,getcwd().'/cert/ca.crt');  //指定证书

curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);  //不验证域名,值2为验证域名

$output=curl_exec($curl);

curl_close($curl);