php curl GET和POST函数示例

新修改的 curl get,如果成功,返回的是一个数组,包含返回的http头信息和数据信息。如果失败返回false。
返回的数据结构可以去掉//var_dump($info);注释查看。
常用的如

$info['http_code'] //http状态码
$info['datas'] //返回的数据信息.

curl get

function curl_get($url,$referer = '')
{
	$UGA="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0";
	$ch = curl_init();
	$headers = array();
 
	$headers[] = "Accept: */*";
	$headers[] = "Cache-Control: max-age=0";
	$headers[] = "Connection: close";
	//$headers[] = "Keep-Alive: 300";
	$headers[] = "Cookie: caches=";
	$headers[] = "Accept-Charset: utf-8;ISO-8859-1;iso-8859-2;q=0.7,*;q=0.7";
	$headers[] = "Accept-Language: en-us,en;q=0.5";
	$headers[] = "Pragma: "; // browsers keep this blank.
	curl_setopt($ch, CURLOPT_USERAGENT, $UGA);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_REFERER, $referer);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT,40);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$datas = curl_exec($ch);
	if(curl_errno($ch))
	{
		echo "error:".curl_error($ch)."\n";
		curl_close($ch);
		return false;
	}
	$info = curl_getinfo($ch);
	$info['datas'] = $datas;
	//var_dump($info);
	curl_close($ch);
	return $info;
}

结构

array(27) {
  ["url"]=>
  string(116) "https://xxx.tumblr.com/video_file/xxxxxx"
  ["content_type"]=>
  string(24) "text/html; charset=UTF-8"
  ["http_code"]=>
  int(302)
  ["header_size"]=>
  int(786)
  ["request_size"]=>
  int(392)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(1.215243)
  ["namelookup_time"]=>
  float(0.028823)
  ["connect_time"]=>
  float(0.029488)
  ["pretransfer_time"]=>
  float(0.901378)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(0)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(1.215218)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(55) "https://vtt.tumblr.com/tumblr_xxxxxx.mp4#_=_"
  ["primary_ip"]=>
  string(14) "199.101.133.63"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(443)
  ["local_ip"]=>
  string(13) "192.168.xx.xxx"
  ["local_port"]=>
  int(52772)
  ["datas"]=>
  string(0) ""
}

curl POST

new:

function curl_post($url,$post_full,$referer = '')
{
	$UGA="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0";
	$ch = curl_init();
	$headers = array();
	$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
	$headers[] = "Accept-Language: q=0.8,en-US;q=0.5,en;q=0.3";
	$headers[] = "Accept-Encoding: gzip, deflate, br";
	$headers[] = "DNT: 1";
  
	$headers[] = "Connection: close";
	$headers[] = "Content-Type: application/x-www-form-urlencoded";
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, $UGA);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_REFERER, $referer);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT,40);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$post_full);
	$datas=curl_exec($ch);
	if(curl_errno($ch))
	{
		echo "error:".curl_error($ch)."\n";
		curl_close($ch);
		return false;
	}
	$info = curl_getinfo($ch);
	$info['datas'] = $datas;
	var_dump($info);
	curl_close($ch);
	return $info;
}

调用:

$url = 'https://console.online.net/en/order/server_limited';
$pdata = 'server_offer=757';
$sdatas = curl_post($url,$pdata,'https://www.online.net/en');
echo $sdatas['datas'];

返回的数据结构可以去掉//var_dump($info);注释查看。
—————————————————
old:

function curl_post($url,$post_full)
{
    $ch = curl_init();
    $headers = array();
 
    $headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    $headers[] = "Accept-Language: q=0.8,en-US;q=0.5,en;q=0.3";
    $headers[] = "Accept-Encoding: gzip, deflate, br";
    $headers[] = "DNT: 1";
 
    $headers[] = "Connection: keep-alive";
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1)");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT,40);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post_full);
    $datas=curl_exec($ch);
 
    curl_close($ch);
    return $datas;
}

——————————————-
curl GET老的代码

function curl_get($url)
{
	$UGA="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)";
	$ch = curl_init();
	$headers = array();

	$headers[] = "Accept: */*";
	$headers[] = "Cache-Control: max-age=0";
	$headers[] = "Connection: keep-alive";
	$headers[] = "Keep-Alive: 300";
	$headers[] = "Cookie: caches=";
	$headers[] = "Accept-Charset: utf-8;ISO-8859-1;iso-8859-2;q=0.7,*;q=0.7";
	$headers[] = "Accept-Language: en-us,en;q=0.5";
	$headers[] = "Pragma: "; // browsers keep this blank.
	curl_setopt($ch, CURLOPT_USERAGENT, $UGA);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_TIMEOUT,40);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$datas=curl_exec($ch);

	curl_close($ch);
	return $datas;
}
上一篇
下一篇