php stream_context_create 无法作用于 stream_socket_client

php中文网
发布: 2016-06-06 20:45:58
原创
1690人浏览过

想通过本地代理,抓取远程网页内容,代码如下:

<?php
$options = array(
    'http'=>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)<br />\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来连接,大家帮我看一下,这个函数,我怎么用错了呢?

回复内容:

想通过本地代理,抓取远程网页内容,代码如下:

<?php
$options = array(
    'http'=>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)<br />\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

立即学习PHP免费学习笔记(深入)”;

DBShop开源商城系统
DBShop开源商城系统

DBShop开源商城系统,使用PHP语言基于Laminas(Zendframework 3) + Doctrine 2 组合框架开发完成。可定制、多终端、多场景、多支付、多货币;严谨的安全机制,可靠稳定;方便的操作管理,节约时间;清晰的权限分配,责任分明;便捷的更新处理,一键搞定;丰富的插件市场,扩展无限。

DBShop开源商城系统 0
查看详情 DBShop开源商城系统

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来连接,大家帮我看一下,这个函数,我怎么用错了呢?

解决了,很难想像,我居然在一个日本的网站找到了答案,所以不要随意抵制日本知识,哈哈!经过测试完全可以实现,你看看:

<?php  
  
$sock = stream_socket_client('tcp://127.0.0.1:8123', $errno, $errstr, 10, STREAM_CLIENT_CONNECT);  
  
if(!$sock) exit();  
  
$request = array();  
$request[] = 'GET http://www.example.com HTTP/1.1';  
$request[] = 'Host: www.example.com';  
$request[] = 'User-Agent: TestClient';  
  
if(!fwrite($sock, implode("\r\n", $request)."\r\n\r\n")){  
    exit();  
}  
  
$response = '';  
while(!feof($sock)){  
    $response .= fgets($sock, 4096);  
}  
  
echo $response;  
  
fclose($sock);  
  
?>
登录后复制

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

说到采集,首先应该想到的是php的curl函数
最好的办法就是模拟爬虫(比如:百度蜘蛛爬虫或者google蜘蛛爬虫),同样也支持代理配置
通过爬虫模拟浏览器的head请求,没有什么抓去不到(理论上只要能通过浏览器请求到的数据,不管是要登录还是不要登录都是可以抓去到内容的)

不确定是不是这个代理有问题,配置个自己的代理看一下?
或者跟进一下? stream_socket_client 空了继续弄吧

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号