Wednesday, November 3, 2021

PHP - How to use proxy to send http requests

 Sometimes we send http requests and need to send it through a proxy server. Here is the way how we can do it in PHP

 $url="http://ip-api.com/json/1.1.1.1";  
 $auth = base64_encode('username:password');  
 $aContext = array(  
   'http' => array(  
     'proxy'      => 'tcp://proxy.server.io:31112',  
     'request_fulluri' => true,  
     'header'     => "Proxy-Authorization: Basic ".$auth,  
   ),  
 );  
 $cxContext = stream_context_create($aContext);  
 $sFile = file_get_contents($url, False, $cxContext);  
 echo $sFile;  

Happy coding...


No comments:

Post a Comment