php stream_context_create 無法作用於 stream_socket_client

來源:互聯網
上載者:User
想通過本地代理,抓取遠程網頁內容,代碼如下:

array(        'proxy'=>'tcp://192.168.1.108:8087',        'request_fulluri '=>true,        "method"  => "GET",         "timeout" => 2,    ),);$context = stream_context_create($options);$fp = stream_socket_client("tcp://www.bigxu.com:80", $errno, $errstr, 30,STREAM_CLIENT_CONNECT,$context);// print_r(stream_context_get_options($fp)); exit;if (!$fp) {    echo "$errstr ($errno)
\n";} else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.bigxu.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp);}?>

php file.php

nginx訪問日誌是:
15.196.206.102 [26/Apr/2014:12:04:45 +0800] http://www.bigxu.com/ 200 20630 0.241 "-" "-"

15.196.206.102 是我的本機IP.
$context沒有起到作用。
代理是絕對可以用的。
因為通過下面代碼,$context會起作用

$options = array(     'http'=>array(         'proxy'=>'tcp://192.168.1.108:8087',         'request_fulluri '=>true,         "method"  => "GET",         "timeout" => 2,     ), ); $context = stream_context_create($options); if ( $fp = fopen("http://www.bigxu.com", 'r', false, $context) ) {     print "well done";    while (!feof($fp)) {         echo fgets($fp, 1024);     } }

php file.php
bigxu.com nginx訪問日誌是:
8.35.201.32 [26/Apr/2014:12:03:03 +0800] http://www.bigxu.com/ 200 7070 0.122 "-" "AppEngine-Google; (+http://code.google.com/appengine; appid: s~goagent0527)"
8.35.201.32 是我的代理IP

比較大型的爬蟲項目,我肯定會用stream_socket_client來串連,大家幫我看一下,這個函數,我怎麼用錯了呢?

回複內容:

想通過本地代理,抓取遠程網頁內容,代碼如下:

array(        'proxy'=>'tcp://192.168.1.108:8087',        'request_fulluri '=>true,        "method"  => "GET",         "timeout" => 2,    ),);$context = stream_context_create($options);$fp = stream_socket_client("tcp://www.bigxu.com:80", $errno, $errstr, 30,STREAM_CLIENT_CONNECT,$context);// print_r(stream_context_get_options($fp)); exit;if (!$fp) {    echo "$errstr ($errno)
\n";} else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.bigxu.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp);}?>

php file.php

nginx訪問日誌是:
15.196.206.102 [26/Apr/2014:12:04:45 +0800] http://www.bigxu.com/ 200 20630 0.241 "-" "-"

15.196.206.102 是我的本機IP.
$context沒有起到作用。
代理是絕對可以用的。
因為通過下面代碼,$context會起作用

$options = array(     'http'=>array(         'proxy'=>'tcp://192.168.1.108:8087',         'request_fulluri '=>true,         "method"  => "GET",         "timeout" => 2,     ), ); $context = stream_context_create($options); if ( $fp = fopen("http://www.bigxu.com", 'r', false, $context) ) {     print "well done";    while (!feof($fp)) {         echo fgets($fp, 1024);     } }

php file.php
bigxu.com nginx訪問日誌是:
8.35.201.32 [26/Apr/2014:12:03:03 +0800] http://www.bigxu.com/ 200 7070 0.122 "-" "AppEngine-Google; (+http://code.google.com/appengine; appid: s~goagent0527)"
8.35.201.32 是我的代理IP

比較大型的爬蟲項目,我肯定會用stream_socket_client來串連,大家幫我看一下,這個函數,我怎麼用錯了呢?

解決了,很難想像,我居然在一個日本的網站找到了答案,所以不要隨意抵制日本知識,哈哈!經過測試完全可以實現,你看看:

原文地址:http://pe5974.sakura.ne.jp/contents/proxy-https.php

說到採集,首先應該想到的是php的curl函數
最好的辦法就是類比爬蟲(比如:百度蜘蛛爬蟲或者google蜘蛛爬蟲),同樣也支援代理配置
通過爬蟲類比瀏覽器的head請求,沒有什麼抓去不到(理論上只要能通過瀏覽器請求到的資料,不管是要登入還是不要登入都是可以抓去到內容的)

不確定是不是這個代理有問題,配置個自己的代理看一下?
或者跟進一下? stream_socket_client 空了繼續弄吧

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.