當前位置:首頁 » 編程語言 » phpcurl網頁內容

phpcurl網頁內容

發布時間: 2022-12-21 17:10:58

php獲取指定網頁內容

一、用file_get_contents函數,以post方式獲取url

<?php

$url='http://www.domain.com/test.php?id=123';

$data=array('foo'=>'bar');

$data= http_build_query($data);

$opts=array(

'http'=>array(

'method'=>'POST',

'header'=>"Content-type: application/x-www-form-urlencoded " .

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式獲取內容

<?php

$url='http://www.domain.com/?para=123';

$html=file_get_contents($url);

echo$html;

?>

三、用fopen打開url, 以get方式獲取內容

<?php

$fp=fopen($url,'r');

$header= stream_get_meta_data($fp);//獲取報頭信息

while(!feof($fp)) {

$result.=fgets($fp, 1024);

}

echo"url header: {$header} <br>":

echo"url body: $result";

fclose($fp);

?>

四、用fopen打開url, 以post方式獲取內容

<?php

$data=array('foo2'=>'bar2','foo3'=>'bar3');

$data= http_build_query($data);

$opts=array(

'http'=>array(

'method'=>'POST',

'header'=>"Content-type: application/x-www-form-

urlencoded Cookie:cook1=c3;cook2=c4 " .

"Content-Length: " .strlen($data) ." ",

'content'=>$data

)

);

$context= stream_context_create($opts);

$html=fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb',false,$context);

$w=fread($html,1024);

echo$w;

?>

五、使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展

<?php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_URL,'http://www.domain.com/');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,$timeout);

$file_contents= curl_exec($ch);

curl_close($ch);

echo$file_contents;

?>

Ⅱ php中curl爬蟲 怎麼樣通過網頁獲取所有鏈接

本文承接上面兩篇,本篇中的示例要調用到前兩篇中的函數,做一個簡單的URL採集。一般php採集網路數據會用file_get_contents、file和cURL。不過據說cURL會比file_get_contents、file更快更專業,更適合採集。今天就試試用cURL來獲取網頁上的所有鏈接。示例如下:

<?php
/*
* 使用curl 採集hao123.com下的所有鏈接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.hao123.com/');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 頁面內容我們並不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回結果,而不是輸出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主機部分,補全用
$host = 'http://www.hao123.com/';
if (is_array($linkarr)) {
foreach ($linkarr as $k => $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("<p>此頁面的所有鏈接為:</p><pre>%s</pre>n", var_export($linkresult , true));
?>

function.php內容如下(即為上兩篇中兩個函數的合集):

<?php
function _striplinks($document) {
preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?>

Ⅲ php curl 抓取頁面幾種方法介紹

使用代理進行抓取
為什麼要使用代理進行抓取呢?以google為例吧,如果去抓google的數據,短時間內抓的很頻繁的話,你就抓取不到了。google對你的ip地址做限制這個時候,你可以換代理重新抓。

代碼如下
<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"
);
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPPROXYTUNNEL,TRUE);
curl_setopt($ch,CURLOPT_PROXY,125.21.23.6:8080);
//url_setopt($ch,CURLOPT_PROXYUSERPWD,'user:password');如果要密碼的話,加上這個
$result=curl_exec($ch);
curl_close($ch);
?>

Ⅳ PHP 如何獲取到一個網頁的內容

1.file_get_contents
PHP代碼

復制代碼 代碼如下:

<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
//如果出現中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>

2.curl
PHP代碼

復制代碼 代碼如下:

<?php
$url = "http://www.jb51.net";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網頁里需要增加下面兩行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>

3.fopen->fread->fclose
PHP代碼

復制代碼 代碼如下:

<?php
$handle = fopen ("http://www.jb51.net", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>

註:
1.
使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設置
allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能打開遠程文件。
2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分
號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴
展。

Ⅳ php curl抓取網頁內容在伺服器上報302Found,怎麼解決

302是重定向狀態碼,響應頭會指定重新跳轉到某個地址,
獲取 302狀態的響應頭 的 Location 欄位的 url地址,重新訪問這個地址就行了。

Ⅵ 如何用php CURL 抓取微信網頁的內容

給你簡單介紹幾個吧
一、file_get_contents函數
$content = file_get_contents("URL");//URL就是你要獲取的頁面的地址
二、利用curl擴展
代碼如下:
function getCurl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不輸出內容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}

PS:需要安裝PHP的curl擴展

Ⅶ php使用curl抓取一個網站的內容被拒絕

剛寫的。希望有用

<?php
$binfo=array('Mozilla/4.0(compatible;MSIE8.0;WindowsNT5.1;Trident/4.0;.NETCLR2.0.50727;InfoPath.2;AskTbPTV/5.17.0.25589;AlexaToolbar)','Mozilla/5.0(WindowsNT5.1;rv:22.0)Gecko/20100101Firefox/22.0','Mozilla/4.0(compatible;MSIE8.0;WindowsNT5.1;Trident/4.0;.NET4.0C;AlexaToolbar)','Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)',$_SERVER['HTTP_USER_AGENT']);
//218.242.124.16*
//125.90.88.*
$cip=餲.242.124.'.mt_rand(0,254);
$xip=餲.242.124.'.mt_rand(0,254);
$header=array(
'CLIENT-IP:'.$cip,
'X-FORWARDED-FOR:'.$xip,
);
functiongetimgs($url,$data,$userinfo,$header)
{
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,"$url");
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_REFERER,");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_USERAGENT,"$userinfo");
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$contents=curl_exec($ch);
curl_close($ch);
return$contents;
}

$url='
$u=$binfo[mt_rand(0,3)];
$data=array(
'keyWords'=>'上海科波',
'searchType'=>Ƈ'
);

$html=(getimgs($url,$data,$u,$header));
//替換鏈接地址
$html=str_replace('href="#"','href=",$html);

echo$html;


?>

Ⅷ php怎麼用curl抓取網頁上的內容

你curl拿到的是整個網頁html,如果想拿某部分內容,需要用正則提取

Ⅸ php獲取網頁源碼內容有哪些辦法

可以參考以下幾種方法:

方法一: file_get_contents獲取

<span style="white-space:pre"></span>$url="http://www..com/";

<span style="white-space:pre"></span>$fh= file_get_contents

('http://www.hxfzzx.com/news/fzfj/');<span style="white-space:pre"></span>echo $fh;

拓展資料

PHP(外文名:PHP: Hypertext Preprocessor,中文名:「超文本預處理器」)是一種通用開源腳本語言。語法吸收了C語言、Java和Perl的特點,利於學習,使用廣泛,主要適用於Web開發領域。PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。它可以比CGI或者Perl更快速地執行動態網頁。

用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標准通用標記語言下的一個應用)文檔中去執行,執行效率比完全生成HTML標記的CGI要高許多;PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

Ⅹ php的curl怎麼爬取網頁內容

  1. 創建一個新cURL資源

  2. 設置URL和相應的選項

  3. 抓取URL並把它傳遞給瀏覽器

  4. 關閉cURL資源,並且釋放系統資源

代碼案例:

熱點內容
我的世界pe伺服器創造 發布:2025-05-15 10:51:17 瀏覽:607
移動端打吃雞要什麼配置 發布:2025-05-15 10:48:16 瀏覽:756
我的世界哪五個伺服器被炸了 發布:2025-05-15 10:36:16 瀏覽:994
ehcache存儲對象 發布:2025-05-15 10:35:31 瀏覽:528
搭建虛擬電腦的伺服器 發布:2025-05-15 10:29:31 瀏覽:270
湖人雙核配置哪個最好 發布:2025-05-15 10:09:48 瀏覽:980
手機熱點密碼怎麼查看 發布:2025-05-15 09:54:47 瀏覽:108
生意發力雲存儲 發布:2025-05-15 09:54:45 瀏覽:617
編寫一個shell腳本添加用戶 發布:2025-05-15 09:54:43 瀏覽:506
資料庫查看錶命令 發布:2025-05-15 09:52:27 瀏覽:915