欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP抓取网页代码示例

程序员文章站 2022-05-22 23:14:53
...
  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