jjzjj

php - 如何为 set socks 5 或 php stream_socket_client 的 http 代理制作上下文数组

coder 2023-09-18 原文

我不确定如何在 php 中为流套接字的 tcp 代理配置流上下文参数。我发现并测试了以下代码,但它不适用于流套接字。

$context = stream_context_create(
            array(
              'http'=>array(
                'proxy'=> 'tcp://'.$proxy,                
                )
              )
            );
$srvHandle = stream_socket_client("tcp://{$this->server}", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($srvHandle === false)
   $this->LogError("failed to connect with host website, check your network connection.");
stream_set_blocking($srvHandle, true);
stream_socket_enable_crypto($srvHandle, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
stream_set_blocking($srvHandle, false);

但是上下文正在为 file_get_contents 函数工作。

$context = stream_context_create(
            array(
              /*
              'socket' => array(
                  'bindto' => $proxy,
                  )    
              */
              'http'=>array(
                'proxy'=> 'tcp://'.$proxy,
                "request_fulluri" => TRUE,
                ),
              "ssl"  => array(
                "SNI_enabled" => FALSE,
                )
              )
            );

$result =  file_get_contents("http://api.ipify.org?format=json", false, $context);

所以我可以知道上下文只适用于 http 协议(protocol)。

如何为 tcp 流套接字配置上下文参数数组?

最佳答案

您可以使用流,根据您的示例,您需要一个客户端流套接字。

stream_socket_client创建一个 TCP 套接字。请注意最后一个参数 $context,它应该使用 stream_context_create 创建。 .

手册上的例子已经够多了,这里主要介绍如何使用

$context = stream_context_create(
            array(
              /*
              'socket' => array(
                  'bindto' => $proxy,
                  )    
              */
              'http'=>array(
                'proxy'=> 'tcp://'.$proxy,
                "request_fulluri" => TRUE,
                ),
              "ssl"  => array(
                "SNI_enabled" => FALSE,
                )
              )
            );

$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);

另请注意 fopen也接受上下文,也可以使用 tcp 流。流扩展函数提供了更多选项,但对于基本使用 fopen 应该足够了。

关于php - 如何为 set socks 5 或 php stream_socket_client 的 http 代理制作上下文数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310932/

有关php - 如何为 set socks 5 或 php stream_socket_client 的 http 代理制作上下文数组的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  3. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  4. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby-on-rails - Rails - 从命名路由中提取 HTTP 动词 - 2

    Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba

  7. ruby-on-rails - Heroku 吃掉了我的自定义 HTTP header - 2

    我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份​​验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby​​脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara

  8. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

  9. ruby-on-rails - 如何为空白字段编写 rspec? [Rails3.1] - 2

    我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona

  10. ruby - HTTP 请求中的用户代理,Ruby - 2

    我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)

随机推荐