如何更改PHP中请求的IP?

时间:2013-02-08 06:40:29

标签: php zend-framework

我正在使用ZEND_HTTP_CLIENT:

$config = array(
  'adapter'      => 'Zend_Http_Client_Adapter_Socket',
  'ssltransport' => 'tls',
  'timeout'      =>  30
);
$client  = new Zend_Http_Client($url , $config);

在我提出此请求时,有没有办法更改我的IP?我目前在服务器上可以使用大约4个IP?

1 个答案:

答案 0 :(得分:1)

您可以通过正在使用的适配器进行更改。如果您正在使用套接字适配器:

$options = array(
    'socket' => array(
        // Bind local socket side to a specific interface
        'bindto' => '10.1.2.3:50505'
    )
);

// Create an adapter object and attach it to the HTTP client
$adapter = new Zend_Http_Client_Adapter_Socket();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);

// Method 1: pass the options array to setStreamContext()
$adapter->setStreamContext($options);

// Method 2: create a stream context and pass it to setStreamContext()
$context = stream_context_create($options);
$adapter->setStreamContext($context);

// Method 3: get the default stream context and set the options on it
$context = $adapter->getStreamContext();
stream_context_set_option($context, $options);

// Now, preform the request
$response = $client->request();

以上内容实际上是从Zend Manual复制/粘贴的。

如果你正在使用Zend的卷曲适配器,你可以传递 CURLOPT_INTERFACE Curl setting

$adapter = new Zend_Http_Client_Adapter_Curl();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);
$adapter->setConfig(array(
    'curloptions' => array(
        CURLOPT_INTERFACE => '192.168.1.2'
    )
));