Parse curl's simple method for submitting GET POST Cookie

  • 2020-06-23 00:03:09
  • OfStack


<?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"
// To send a file, prefix the file name @ Prefix and use the full path. 
// Using an array to provide post The data, CURL Components are presumably meant for compatibility @filename This type of upload file writing, default to content_type Set to multipart/form-data . 
// Although for the most part web Servers are not affected, but a few are not compatible. 
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));
// If you don't need to upload a file, try to be right post The submitted data is carried out http_build_query , and then sent out, can achieve better compatibility, smaller request packets. 
$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;

Related articles: