PHP_CURL库函数

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ahaotata/article/details/83793791

你可能在你的编写PHP脚本代码中会遇到这样的问题:怎么样才能从其他站点获取内容呢?这里有几个解决方式;最简单的就是在php中使用fopen()函数,但是fopen函数没有足够的参数来使用,比如当你想构建一个“网络爬虫”,想定义爬虫的客户端描述(IE,firefox),通过不同的请求方式来获取内容,比如POST,GET;等等这些需求是不可能用fopen() 函数实现的。

curl 模拟浏览器请求,比如获取远程浏览器内容,虽然可以用file_get-contents来代替,但curl还支持浏览器类型,cookie和来源ip等,功能相比强大

常用相关函数

curl_init() 初始化curl会话

curl_setopt() 设置curl传输选项

curl_exec 执行curl会话

curl_errno 返回最后一次的错误代码

curl_error 返回当前会话嘴周一次错误的字符串

curl_close 关闭curl会话

例如

<?php
// 1. 初始化
 $ch = curl_init();
 // 2. 设置选项,包括URL
 curl_setopt($ch,CURLOPT_URL,"http://www.devdo.net");
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_HEADER,0);
 // 3. 执行并获取HTML文档内容
 $output = curl_exec($ch);
 if($output === FALSE ){
 echo "CURL Error:".curl_error($ch);
 }
 // 4. 释放curl句柄
 curl_close($ch);
 ?>
<?php
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, “http://www.google.com/does/not/exist”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL
$output = curl_exec($ch);
// Get response code
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Not found?
if ($response_code == ‘404′) {
echo ‘Page doesn\’t exist’;
} else {
echo $output;
}
?>

加深理解:

function getCurl($url,$cookie = null,$post_data = null,$miao = null){
 
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	
	
	if($cookie)
	{
		
		if($cookie =='auto')
		{
			$cookie_string = '';
			$_COOKIE['user_id'] = 12;
			foreach($_COOKIE as $key=>$val)
			{
				$cookie_string .= $key.'='.$val.';';
			}
			curl_setopt($ch, CURLOPT_COOKIE,$cookie_string);
		}
		elseif(is_array($cookie) && $cookie['type']=='cookie_file')
		{
			//echo $cookie['value'];
			curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie['value']);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie['value']);
		}
		else
		{
			curl_setopt($ch, CURLOPT_COOKIE,$cookie);
		}
	}
	if($post_data)
	{
		curl_setopt($ch, CURLOPT_POST, 1);
 
		// 把post的变量加上
 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	}
	
	//判断超时处理
	if(!empty($miao)){
		curl_setopt($ch, CURLOPT_NOSIGNAL, 1);    //注意,毫秒超时一定要设置这个
		curl_setopt($ch, CURLOPT_TIMEOUT_MS, $miao);  //超时毫秒,cURL 7.16.2中被加入。从PHP 5.2.3起可使用	
		$head = curl_exec($ch);
		$curl_errno = curl_errno($ch);
		$curl_error = curl_error($ch);
		if($curl_errno>0){
			$head = '2';
		}else{
			$head = $head;
		}
	}else{
		$head = curl_exec($ch);
	}
 
 
 
    //$head = curl_exec($ch);
    //echo $url;echo 'bb';var_dump($head);exit;
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch); 
	return $head;
}
展开阅读全文

php_curl无法载入的问题

08-16

php.ini中改了,还是出现这个问题(当然了,我的路径中式存在这个扩展的)rnrn[color=#FF0000]httpd.exe: Could not reliably determine the server's fully qualified domain name, using 192.168.2.101 for ServerNamernPHP Warning: PHP Startup: Unable to load dynamic library 'd:/server/php/ext\\php_curl.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0rn[Thu Aug 16 22:36:43 2012] [notice] Apache/2.2.19 (Win64) PHP/5.2.5 (x64) configured -- resuming normal operationsrn[Thu Aug 16 22:36:43 2012] [notice] Server built: May 28 2011 15:18:56rn[Thu Aug 16 22:36:43 2012] [notice] Parent: Created child process 10324rnhttpd.exe: Could not reliably determine the server's fully qualified domain name, using 192.168.2.101 for ServerNamernhttpd.exe: Could not reliably determine the server's fully qualified domain name, using 192.168.2.101 for ServerNamernPHP Warning: PHP Startup: Unable to load dynamic library 'd:/server/php/ext\\php_curl.dll' - \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3\r\n in Unknown on line 0rn[Thu Aug 16 22:36:43 2012] [notice] Child 10324: Child process is runningrn[Thu Aug 16 22:36:43 2012] [notice] Child 10324: Acquired the start mutex.rn[Thu Aug 16 22:36:43 2012] [notice] Child 10324: Starting 64 worker threads.rn[Thu Aug 16 22:36:43 2012] [notice] Child 10324: Starting thread to listen on port 80.[/color]rnrn如果我把依赖扩展在apache配置文件中加载的话apache会启动不了(apache配置中预加载mysql的依赖没问题)rnrn这个是我用x64的apache和x64的php遇到的问题,求遇到过的指点迷津 论坛

没有更多推荐了,返回首页