PHP fsockopen/curl怎么获取目标转向后的页面代码有关问题

php中文网
发布: 2016-06-13 13:23:26
原创
758人浏览过

PHP fsockopen/curl如何获取目标转向后的页面代码问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<? 
$ghurl = isset($_GET['id']) ? $_GET['id']:'http://3gabc.com/'; 
// php 获取 
function getContents($url){ 
$header = array("Referer: http://3gabc.com/"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_HTTPHEADER,$header); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);  //是否抓取跳转后的页面
ob_start(); 
curl_exec($ch); 
$contents = ob_get_contents(); 
ob_end_clean(); 
curl_close($ch); 

return $contents; 
} 

$contents = getContents($ghurl); 
echo $contents; 
?> 

登录后复制


失败。。。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php
function get_page_content($url){
 $url = eregi_replace('^http://', '', $url);
 $temp = explode('/', $url);
 $host = array_shift($temp);
 $path = '/'.implode('/', $temp);
 $temp = explode(':', $host);
 $host = $temp[0];
 $port = isset($temp[1]) ? $temp[1] : 80;
 $fp = @fsockopen($host, $port, &$errno, &$errstr, 30);
 if ($fp){
     @fputs($fp, "GET ".$path." HTTP/1.1\r\nHost: ".$host." \r\nAccept: */*\r\nReferer:".$url." \r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nConnection: Close\r\n\r\n");
 }
 $Content = '';
 while ($str = @fread($fp, 4096)){
     $Content .= $str;
 }
 @fclose($fp);
 //echo $Content;
 //重定向
 if(preg_match("/^HTTP\/\d.\d 301 Moved Permanently/is",$Content)){
  if(preg_match("/Location:\s+(.*?)\s+/is",$Content,$murl)){ 
      return get_page_content($url."/".$murl[1]);
  }
 }

 //读取内容
 if(preg_match("/^HTTP\/\d.\d 200 OK/is",$Content)){
  preg_match("/Content-Type:(.*?)\r\n/is",$Content,$murl);
  $contentType=trim($murl[1]);
  $Content=explode("\r\n\r\n",$Content,2);
  $Content=$Content[1];
 }
 return $Content;
}


echo get_page_content('3gabc.com');

?>

登录后复制


失败。。。

先后 尝试fsockopen/curl等方法,获取header并判断执行,但都失败,请教各位。

------解决方案--------------------


3gabc.com就只有这么一句。
你也只能获取到这个,meta refresh是在浏览器上执行的!!!

------解决方案--------------------
因为3gabc.com就只有这么一句。

你在抓取页面后 “echo $contents;” 页面自然就重定向到http://www.3Gabc.com 了。
所以不能echo $contents; 而是用正则“preg_match("//is",$content, $matches)”
抓出转向地址,然后在curl这个转线地址,就可以抓到你要的内容了。
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号