当前位置:首页 » 编程语言 » 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资源,并且释放系统资源

代码案例:

热点内容
c语言编译后图片 发布:2025-05-15 13:51:57 浏览:792
没有被调用的函数会参与编译吗 发布:2025-05-15 13:42:51 浏览:260
在计算机中ftp的中文 发布:2025-05-15 13:41:07 浏览:1000
国网校招要网签密码和账号干什么 发布:2025-05-15 13:40:25 浏览:179
java分 发布:2025-05-15 13:34:36 浏览:846
如何下载卡巴斯基安卓版 发布:2025-05-15 13:34:36 浏览:480
排序函数c语言 发布:2025-05-15 13:06:28 浏览:6
韩服lol挂机脚本 发布:2025-05-15 12:42:56 浏览:462
监控存储服务器如何调试 发布:2025-05-15 12:36:30 浏览:219
一万级净化车间有哪些配置 发布:2025-05-15 12:16:41 浏览:98