Skip to content

Instantly share code, notes, and snippets.

@zsmynl
Created February 7, 2014 21:50
Show Gist options
  • Save zsmynl/8872677 to your computer and use it in GitHub Desktop.
Save zsmynl/8872677 to your computer and use it in GitHub Desktop.
php使用curl进行post数据;
<?php
function request($url,$data='',$headers=array()) {
if(empty($headers['UserAgent'])) $headers['UserAgent']='Mozilla/5.0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
if($data){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_USERAGENT, $headers['UserAgent']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//新添加代码(zm)
if(!empty($headers['COOKIE'])){
curl_setopt($ch, CURLOPT_COOKIE, $headers['COOKIE']);
}
$requestHeaders=array(
'Accept-Language: zh-CN',
'Connection: keep-alive',
'Cache-Control: no-cache'
);
if(!empty($headers['Referer'])) $requestHeaders['Referer']=$headers['Referer'];
curl_setopt($ch,CURLOPT_HTTPHEADER,$requestHeaders);
$result=curl_exec($ch);
$error=curl_errno($ch);
$request=curl_getinfo($ch,CURLINFO_HEADER_OUT);
curl_close($ch);
if(is_int($error) && $error>0) return array('error'=>$error);
return array('request'=>$request,'response'=>$result);
}
?>
<?php
request($url,$data);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment