首页 > 其他 > 网站日记>正文

curl的使用集锦【php】

提示: 阅读权限:公开  
 
  
// 发送请求  注意 CURLOPT_FOLLOWLOCATION的含义
function http_request($url, $post_data = array(), $header = 'Content-Type: application/json') {
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	// 返回最后的Location
	curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
	curl_setopt ( $ch, CURLOPT_POST, 1 );
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
	curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
			$header,
			'Content-Length: ' . strlen ( $post_data )
	) );
	$contents = curl_exec ( $ch );
	curl_close ( $ch );
	return $contents;

下边的代码,用来查询百度是否收录网页:
$myweb="http://www.zhongyf.com";
function checkBaidu($url) {
    $url = 'http://www.baidu.com/s?wd=' . $url;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $rs = curl_exec($curl);
    curl_close($curl);

    if (strpos($rs, '很抱歉,没有找到与')) {
     return 0;
    } else {
     return 1;
    }
}
function curl_get_contents($url)   
{   
    if(!function_exists('curl_init'))return file_get_contents($url);
    $ch = curl_init();   
    curl_setopt($ch, CURLOPT_URL, $url);            //设置访问的url地址   
    //curl_setopt($ch,CURLOPT_HEADER,1);            //是否显示头部信息
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);           //设置超时   
    curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);   //用户访问代理 User-Agent   
    curl_setopt($ch, CURLOPT_REFERER,_REFERER_);        //设置 referer   
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);      //跟踪301   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //返回结果到变量而不是直接输出   
    $r = curl_exec($ch);   
    curl_close($ch);   
    return $r;   
}

上一篇:帝国cms站长群

下一篇:useragent两千条,爬虫专用

tags:

返回首页

相关

热门

站内直通车

[!--temp.bottomnav--]
返回顶部