Home  >  Article  >  Backend Development  >  A simple method to parse curl to submit GET, POST, and Cookie_PHP Tutorial

A simple method to parse curl to submit GET, POST, and Cookie_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:361174browse

Copy code The code is as follows:

$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"
//To send a file, prefix the file name with @ and use the full path.
//When using an array to provide post data, the CURL component is probably to be compatible with the @filename writing method for uploading files. By default, the content_type is set to multipart/form-data.
//Although it has no impact on most web servers, there are still a small number of servers that are incompatible.
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));
//When there is no need to upload files, try to http_build_query the data submitted by post, and then send it out, which can achieve better compatibility and smaller requests packet.
$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;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327924.htmlTechArticleCopy the code as follows: ?php $get_data = array ( "get1" = "get1", "get2" = "get2", "get3" = "get3" ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://test.test.com/t...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn