下面直接给出 使用 curl 和 fsockopen 的例子,你可以当成使用实例,也可以直接当作封装好的函数直接使用。
curl函数使用代码
http get 请求:
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
http post 请求,等待返回结果:(当然你可以通过设置timeout来达到异步的效果: curl_setopt(
public function xcurl($url,$ref=null,$post=array(),$ua="Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
if(!empty($ref)) {
curl_setopt($ch, CURLOPT_REFERER, $ref);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!empty($ua)) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
}
if(count($post) > 0){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$output = curl_exec($ch);
curl_close($ch);
if($print) {
print($output);
} else {
return $output;
}
}
fsockopen 函数使用代码:(异步post 请求)
public function curl_request_async($url, $params, $type='GET')
{
// set referer
$referer = $_SERVER['HTTP_HOST'];
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$parts=parse_url($url);
@$fp = fsockopen($parts['host'], isset($parts['port'])?$parts['port']:80, $errno, $errstr, 2);
if(!$fp){
echo "$errstr ($errno)<br />\n";
}else{
if('GET' == $type) $parts['path'] .= '?'.$post_string;
$out = "$type ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Referer: ".$referer."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Connection: Close\r\n\r\n";
// Data goes in the request body for a POST request
if ('POST' == $type && isset($post_string)) $out.= $post_string;
fwrite($fp, $out);
/* // 打开这段请求需要等待结果返回
$receive = '';
while (!feof($fp)) {
$receive .= fgets($fp, 128);
}
echo "<br />".$receive;
*/
usleep(3000); // 不加这段有的时候服务器接收不到请求。大坑.
fclose($fp);
}
}
补充:
php post json数据:
function jcurl($url,$ref=null,$post=array(),$ua="Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre",$print=false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
if(!empty($ref)) {
curl_setopt($ch, CURLOPT_REFERER, $ref);
}
curl_setopt($ch, CURLOPT_URL, $url);
if(!empty($ua)) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
}
if(is_array($post)){
$data_string = json_encode($post);
}else{
$data_string = $post;
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
$output = curl_exec($ch);
curl_close($ch);
if($print) {
print($output);
} else {
return $output;
}
}
这种方式 不能使用 $_POST 接收变量,需要通过
file_get_contents("php://input")
或
$GLOBALS['HTTP_RAW_POST_DATA'];
获取 post 变量
此文章通过 python 爬虫创建,原文是自己的csdn 地址: php curl , fsockopen 函数