cnerta/behind-a-proxy-bundle

此包已被废弃,不再维护。未建议替代包。

使用流上下文添加 CURL、SoapClient 连接和 PHP 函数的代理参数。

安装: 540

依赖项: 0

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 0

类型:symfony-bundle

2.0.1 2016-04-13 07:10 UTC

This package is not auto-updated.

Last update: 2020-02-02 16:31:22 UTC


README

Cnerta Behind A Proxy Bundle

使用流上下文添加 CURL、SoapClient 连接和 PHP 函数的代理参数。

SensioLabsInsight

Build Status Latest Stable Version Latest Unstable Version

警告 host_ssl 配置自 2.0.0 版本以来已被移除
对于 PHP 5.4 及以下版本,请使用该包的 1 版本

安装包

  1. 在您的 composer.json 中添加源
     "require": {
        // ...
        "cnerta/behind-a-proxy-bundle": "1.0.*"
    }
  1. 然后将它添加到您的 AppKernel 类中:
    // in AppKernel::registerBundles()
    $bundles = array(
        // ...
        new Cnerta\BehindAProxyBundle\CnertaBehindAProxyBundle(),
        // ...
    );

配置

config.yml

    cnerta_behind_a_proxy:
        enabled: false                # type: boulean, default value: false, desc: enabled (true), or desabled (false) the use of proxy
        host: 127.0.0.1               # type: string, default value: null, desc : this is the IP or URL of the proxy server
        port: 80                      # type: mixed(string|int), default value: null, desc : this is the port of the proxy server
        login: myWonderfulLogin       # type: string, default value: null, desc : this is the login for authentication against the proxy server
        password: myWonderfulLogin    # type: string, default value: null, this is the password for authentication against the proxy server
        load_default_stream_context: false    # type: boolean, default value: false, If you need to set the default proxy config global
        http:       # Option set only for http request
            host_proxy: 127.0.0.1     # type: string, default value: null, desc : this is the IP or URL of the proxy server
            port_proxy: 80            # type: mixed(string|int), default value: null, desc : this is the port of the proxy server
            login_proxy: login        # type: string, default value: null, desc : this is the login for authentication against the proxy server
            password_proxy: password  # type: string, default value: null, this is the password for authentication against the proxy server
            request_fulluri: true     # type: boulean, default value: false, desc: enabled (true), or desabled (false) the full uri option of context
        https:      # Option set only for https request
            host_proxy: 127.0.0.1     # type: string, default value: null, desc : this is the IP or URL of the proxy server
            port_proxy: 80            # type: mixed(string|int), default value: null, desc : this is the port of the proxy server
            login_proxy: login        # type: string, default value: null, desc : this is the login for authentication against the proxy server
            password_proxy: password  # type: string, default value: null, this is the password for authentication against the proxy server
            request_fulluri: true     # type: boulean, default value: false, desc: enabled (true), or desabled (false) the full uri option of context

设置 CURL 代理配置

    use Symfony\Component\DependencyInjection\ContainerInterface;
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    //...

    $s = curl_init();
    curl_setopt($s, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($s, CURLOPT_FAILONERROR, true);
    curl_setopt($s, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($s, CURLOPT_URL, $this->url);

    // Call cnerta.baproxy service and call the method setProxyForCURL
    // the CURL resource '$s' is passed by reference
    $container->get('cnerta.baproxy')->setProxyForCURL($s);

    curl_exec($s);
    $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
    $error = curl_error($s);

    curl_close($s);

    if ($status == 401) {
        throw new \RuntimeException("Invalid Credencial to connect to WebService");
    } else if ($status == 404) {
        throw new \RuntimeException("Invalid URL to connect to WebService");
    } elseif ($status != 200) {
        throw new \RuntimeException($error);
    }

设置 SoapClient 代理配置


    use Symfony\Component\DependencyInjection\ContainerInterface;
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    //...

    $config =  array(
        "trace" => true,
        "exceptions" => 0,
        "cache_wsdl" => WSDL_CACHE_NONE
    );

    $container->get('cnerta.baproxy')->setProxyForSoapClient($config);

    $soapClient = new \SoapClient('http://www.somewhere.com/?wsdl', $config);

在任何地方获取参数

    use Symfony\Component\DependencyInjection\ContainerInterface;
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    private $container;

    //...

    $this->container->getParameter("cnerta_baproxy.enabled")
    $this->container->getParameter("cnerta_baproxy.host")
    $this->container->getParameter("cnerta_baproxy.port")
    $this->container->getParameter("cnerta_baproxy.host_ssl")
    $this->container->getParameter("cnerta_baproxy.login")
    $this->container->getParameter("cnerta_baproxy.password")
    $this->container->getParameter("cnerta_baproxy.http")
    $this->container->getParameter("cnerta_baproxy.https")