Tuesday, September 25, 2012

Add Proxy Context Stream For PHP

Using Curl:

 <?php
    $proxyauth = 'username:pwd';   
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_PROXY, '10.0.0.125:8080');
    curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   
    $content = curl_exec($ch);
    curl_close($ch);

?>

No Credential require your proxy, remove the following line

curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   

Using Context Stream:

<?php
$url = 'http://www.google.com';
$proxy = 'tcp://10.0.0.100:8080';
$auth = base64_encode('username:pwd');
$context = array(
'http' => array(
        'proxy' => $proxy,
        'header' => "Proxy-Authorization: Basic $auth",
        'request_fulluri' => True,
        ),
);
$context = stream_context_create($context);
echo $body = file_get_contents($url, False, $context);

?>  

No Credential require your proxy, remove the following line 

'header' => "Proxy-Authorization: Basic $auth", 

Thats it 

No comments:

Post a Comment