>  기사  >  백엔드 개발  >  PHP抓取网页代码示例

PHP抓取网页代码示例

WBOY
WBOY원래의
2016-07-25 08:46:10987검색
  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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.