当前位置:Gxlcms > PHP教程 > PHP抓取网页内容的技巧分享_PHP教程

PHP抓取网页内容的技巧分享_PHP教程

时间:2021-07-01 10:21:17 帮助过:2人阅读

如何才能正确的实现可为什么PHP抓取网页内容后没反应呢?连测试的文字都没有,要是我把echo "测试一下";放到第一行就可以输出,我估计是curl_init()函数还没有运行!

你看看PHP的phpinfo()中有没有CURL扩展支持!

把php_curl.dll拷到c:windows和c:windowssystem32里面 重启apache之后再试试看

不是php_curl.dll这个文件,是把php目录中的libeay32.dll,ssleay32.dll拷到c:windowssystem32里面 重启apache

为了服务器安全着想,所以把allow_url_fopen关掉了。

当服务器allow_url_fopen = Off 时,就不能用file_get_contents,只有设置ON时可以用。

  1. < ?php /*
  2. $getstr=file_get_contents("http://www.
    163.com/weatherxml/54511.xml");
  3. $qx=explode(""",strstr($getstr,"qx="));
  4. $wd=explode(""",strstr($getstr,"wd="));
  5. $qximg=explode(""",strstr($getstr,"qximg="));
  6. $qximg_=explode(",",$qximg[1]);
  7. echo "北京 ".$qx[1]."";
  8. echo $wd[1];*/
  9. //echo "< img src='http://news.
    163.com/img/logo/".$qximg_[0]."'
    >
    < img src='http://news.163.com
    /img/logo/".$qximg_[1]."'
    >";
  10. ?>

以下PHP抓取网页内容的范例是通curl_init函数来获取163天气预报

把php.ini里( ;extension=php_curl.dll ) 前面的(;)去掉保存

把php_curl.dll,libeay32.dll,ssleay32.dll拷到c:windowssystem32里,重启IIS即可,没有装apache

  1. < ?php
  2. //初始化curl
  3. $ch = curl_init() or die (curl_error());
  4. //设置URL参数
  5. curl_setopt($ch,CURLOPT_URL,"http:
    //www.163.com/weatherxml/54511.xml");
  6. //要求CURL返回数据
  7. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  8. //执行请求
  9. $result = curl_exec($ch) or die (curl_error());
  10. //取得返回的结果,并显示
  11. //echo $result;
  12. // echo curl_error($ch);
  13. $qx=explode(""",strstr($result,"qx="));
  14. $wd=explode(""",strstr($result,"wd="));
  15. $qximg=explode(""",strstr($result,"qximg="));
  16. $qximg_=explode(",",$qximg[1]);
  17. echo "北京 ".$qx[1]."< br />";
  18. echo $wd[1];
  19. //关闭CURL
  20. curl_close($ch);
  21. ?>

通过以上对PHP抓取网页内容的学习,大家可以自行实际操作一遍,加深对它的理解。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446179.htmlTechArticle如何才能正确的实现 可为什么PHP抓取网页内容后没反应呢?连测试的文字都没有,要是我把echo 测试一下;放到第一行就可以输出,我估计是...

人气教程排行