php中使用Curl、socket、file_get_contents三种方法POST提交数据
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求也不大,所以没有学习使用curl。 php中curl和file_get_content的一些比较主要区别: 学习才发现,curl支持很多协议,有FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE以及LDAP,也就是说,它能做到很多file_get_content做不到的事情。curl在php可以实现远程获取和采集内容;实现PHP网页版的FTP上传下载;实现模拟登陆;实现接口对接(API),数据传输;实现模拟Cookie;下载文件断点续传等等,功能十分强大。 了解curl一些基本的使用后,才发现其实并不难,只不过记住里面一些设置参数,难弄一点,但是我们记住几个常用的就可以了。 开启curl: 因为PHP默认是不支持curl功能的,因此如果要用curl的话,首先需要在php.ini中开启该功能,即去掉 ;extension= php_curl.dll 前面的分号,然后保存后重启apache/iis就好了。 基本语法: 代码如下: $my_curl = curl_init(); //初始化一个curl对象
curl_setopt($my_curl,CURLOPT_URL,"//www.52php.cn"); //设置你需要抓取的URL curl_setopt($my_curl,CURLOPT_RETURNTRANSFER,1); //设置是将结果保存到字符串中还是输出到屏幕上,1表示将结果保存到字符串 $str = curl_exec($curl); //执行请求 echo $str; //输出抓取的结果 curl_close($curl); //关闭url请求 最近需要获取别人网站上的音乐数据。用了file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效: $config['context'] = stream_context_create(array('http' => array('method' => "GET", 这时候,看一下服务器的连接池,会发现一堆类似的错误,让我头疼万分: 现在改用了curl库,写了一个函数替换: 如此,除了真正的网络问题外,没再出现任何问题。 2.31319094 curl使用的时间: 0.68719101 差距很大?呵呵,从我使用的经验来说,这两个工具不只是速度有差异,稳定性也相差很大。 建议对网络数据抓取稳定性要求比较高的朋友使用上面的 curl_file_get_contents函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦! 方法1: 用file_get_contents 以get方式获取内容 代码如下: $url='http://www.domain.com/';
$html = file_get_contents($url); echo $html; ?> 方法2: 用fopen打开url,以get方式获取内容 代码如下: $fp = fopen($url,'r');
stream_get_meta_data($fp); while(!feof($fp)) { $result .= fgets($fp,1024); } echo "url body: $result"; fclose($fp); ?> 方法3:用file_get_contents函数,以post方式获取url 代码如下: $data = array ('foo' => 'bar');
$data = http_build_query($data); $opts = array ( 方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body 代码如下: function get_url ($url,$cookie=false)
{ $url = parse_url($url); $query = $url[path]."?".$url[query]; echo "Query:".$query; $fp = fsockopen( $url[host],$url[port]?$url[port]:80,$errno,$errstr,30); if (!$fp) { return false; } else { $request = "GET $query HTTP/1.1rn"; $request .= "Host: $url[host]rn"; $request .= "Connection: Closern"; if($cookie) $request.="Cookie: $cookien"; $request.="rn"; fwrite($fp,$request); while()) { $result .= @fgets($fp,1024); } fclose($fp); return $result; } } //获取url的html部分,去掉header function GetUrlHTML($url,$cookie=false) { $rowdata = get_url($url,$cookie); if($rowdata) { $body= stristr($rowdata,"rnrn"); $body=substr($body,4,strlen($body)); return $body; } return false; } ?> 方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body 代码如下: function HTTP_Post($URL,$data,$cookie,$referrer="")
{ // parsing the given URL $URL_Info=parse_url($URL); // Building referrer // making string from $data // Find out which port is needed – if not given use standard (=80) // building POST-request: $request.="Cookie: $cookien"; $request.="n"; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); return $result; 方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展 代码如下: $ch = curl_init();
$timeout = 5; curl_setopt ($ch,'http://www.domain.com/'); curl_setopt ($ch,1); curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents; ?> php中 curl, fsockopen ,file_get_contents 三个函数 都可以实现采集模拟发言 。三者有什么区别,或者讲究么 赵永斌: 范佳鹏: (远程)我个人理解到的表述如下(不对请指出,不到位请补充) 潘少宁-腾讯: 代码如下: /** * Socket版本 * 使用方法: * $post_string = "app=socket&version=beta"; * request_by_socket('52php.cn','/restServer.php',$post_string); */ function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){ $socket = fsockopen($remote_server,$port,$timeout); if (!$socket) die("$errstr($errno)"); fwrite($socket,"POST $remote_path HTTP/1.0"); fwrite($socket,"User-Agent: Socket Example"); fwrite($socket,"HOST: $remote_server"); fwrite($socket,"Content-type: application/x-www-form-urlencoded"); fwrite($socket,"Content-length: ".strlen($post_string)+8.""); fwrite($socket,"Accept:*/*"); fwrite($socket,""); fwrite($socket,"mypost=$post_string"); fwrite($socket,""); $header = ""; while ($str = trim(fgets($socket,4096))) { $header.=$str; } $data = ""; while (!feof($socket)) { $data .= fgets($socket,4096); } return $data; } /** * Curl版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_curl('http://52php.cn/restServer.php',$post_string); */ function request_by_curl($remote_server,$post_string){ $ch = curl_init(); curl_setopt($ch,$remote_server); curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string); curl_setopt($ch,true); curl_setopt($ch,"Jimmy's CURL Example beta"); $data = curl_exec($ch); curl_close($ch); return $data; } /** * 其它版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_other('http://52php.cn/restServer.php',$post_string); */ function request_by_other($remote_server,$post_string){ $context = array( 'http'=>array( 'method'=>'POST', 'header'=>'Content-type: application/x-www-form-urlencoded'."". 'User-Agent : Jimmy's POST Example beta'."". 'Content-length: '.strlen($post_string)+8, 'content'=>'mypost='.$post_string) ); $stream_context = stream_context_create($context); $data = file_get_contents($remote_server,FALSE,$stream_context); return $data; } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |