苦苦搜寻没有结果,
请问带cookie访问个网站,读取源码,处理保存到文件或输出出来呢,新手请教不胜感激。
我自己写的,这个但是只能输出原页面效果,看不到源码。。。加了个循环却无法工作。。
1 | <?php $url = 'http://weibo.com/***/follow' ; $data = array ( 'foo' => 'bar' ); $data = http_build_query( $data ); $opts = array ( 'http' => array ( 'method' => 'GET' , 'header' => "Content-type: text/html" . "Content-Length: " . strlen ( $data ) . "\r\n" . "cookie:***\r\n" , 'content' => $data ) ); $context = stream_context_create( $opts ); $html = file_get_contents ( $url , '' , $context ); //var_dump( $html ); $lines = file( $html );//我估计这行可能写的不对//var_dump( $lines ); foreach ( $lines as $key => $value ){ echo "{$key}:" .htmlspecialchars( $value ). "<br>\n" ;}?>
|
Salin selepas log masuk
回复讨论(解决方案)
你 $html = file_get_contents($url,'',$context); 后
$html 中不就是文件内容了吗?
echo $html;
看一下就知道了
谢版主回复,echo $html 我试过了,浏览器刷新出来的整个页面,而不是我想要的源码效果。
就好比
1 | <?php $url = "http://www.baidu.com/" ; $lines = file( $url ); foreach ( $lines as $key => $value ){ echo "{$key}:" .htmlspecialchars( $value ). "<br>\n" ;}?>
|
Salin selepas log masuk
输出的结果就是全部的源码,是想要这样的。不知道我的描述是否清楚。
自己把$html保存到一个文件,再用文本编辑器打开看看
自己把$html保存到一个文件,再用文本编辑器打开看看
你说的方法刚才试了一下是可以的,然后参照网上别人的实例,直接file_get_contents就好了,但是乱码的问题。。。
1 | <?php $url = 'http://weibo.com/**/follow' ; $data = array ( 'foo' => 'bar' ); $data = http_build_query( $data ); $opts = array ( 'http' => array ( 'method' => 'GET' , 'header' => "Content-type: text/html" . "Content-Length: " . strlen ( $data ) . "\r\n" . "cookie:**\r\n" , 'content' => $data ) ); $context = stream_context_create( $opts ); $html = file_get_contents ( $url , '' , $context ); // $source = htmlspecialchars( $html );// $code = preg_match(pattern, subject) echo htmlspecialchars( $html );//var_dump( $html );// $lines = file( $url );// $lines = file_get_contents ( $url );//var_dump( $lines );// $files = fopen ( "text.txt" , "w" );//fwrite( $files , $lines ); //fclose( $lines );?>
|
Salin selepas log masuk
乱码是因为你的编码没有统一,用header设置下字符集。
header("content-type:text/html;charset=utf-8");