当前位置:Gxlcms > PHP教程 > PHP抓取网页代码示例

PHP抓取网页代码示例

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

  1. //PHP(前提是装了curl):
  2. $ch = curl_init();
  3. curl_setopt ($ch, CURLOPT_URL, "http://www.xxx/");
  4. curl_setopt ($ch, CURLOPT_REFERER, "http://www.xxx/");
  5. curl_exec ($ch);
  6. curl_close ($ch);
  7. //PHP(不装curl用sock)
  8. $server = 'blog.qita.in';
  9. $host = 'blog.qita.in';
  10. $target = '/xxx.asp';
  11. $referer = 'http://blog.qita.in/'; // Referer
  12. $port = 80;
  13. $fp = fsockopen($server, $port, $errno, $errstr, 30);
  14. if (!$fp)
  15. {
  16. echo "$errstr ($errno)
    \n";
  17. }
  18. else
  19. {
  20. $out = "GET $target HTTP/1.1\r\n";
  21. $out .= "Host: $host\r\n";
  22. $out .= "Cookie: ASPSESSIONIDSQTBQSDA=DFCAPKLBBFICDAFMHNKIGKEG\r\n";
  23. $out .= "Referer: $referer\r\n";
  24. $out .= "Connection: Close\r\n\r\n";
  25. fwrite($fp, $out);
  26. while (!feof($fp))
  27. {
  28. echo fgets($fp, 128);
  29. }
  30. fclose($fp);
  31. }

网页代码, PHP

人气教程排行