首页 >web前端 >html教程 >curl,fsocketopen,socket 三种函数抓取html页面_html/css_WEB-ITnose

curl,fsocketopen,socket 三种函数抓取html页面_html/css_WEB-ITnose

WBOY
WBOY原创
2016-06-24 11:39:121056浏览

(1) php - curl 

<?php $ch_article = curl_init();    $url        = 'www.baidu.com';    curl_setopt($ch_article, CURLOPT_URL, $url);    curl_setopt($ch_article, CURLOPT_RETURNTRANSFER, 0);    curl_setopt($ch_article, CURLOPT_HEADER, 0);    $article_output = curl_exec($ch_article);    curl_close($ch_article);    echo $article_output;?>


(2) php - fsocketopen

<?php $fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);$out = "GET / HTTP/1.1\r\n";$out .= "Host: www.baidu.com\r\n";$out .= "Connection: Close\r\n\r\n";fwrite($fp, $out);while (!feof($fp)) {    echo fgets($fp, 128);}fclose($fp);?>


(3) php - socket

<?php $url='www.baidu.com';$Port = 80;$host_ip  = gethostbyname('www.baidu.com');$Header  .= trim('Host:www.baidu.com')."\r\n";$Header  .= trim('Connection: Close')."\r\n";$method   = 'GET';$Request  = $method." " . '/' . " HTTP/1.1\r\n";$Request .= $Header;$Request .= "\r\n";$sockHttp    = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);$resSockHttp = socket_connect($sockHttp, $host_ip, $Port);socket_write($sockHttp, $Request, strlen($Request));$Response = '';while ($Read_data = socket_read($sockHttp, 4096)){	$Response .= $Read_data;}socket_close($sockHttp);echo $Response;?>


版权声明:本文为博主原创文章,未经博主允许不得转载。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn