PHP cURL vs. file_get_contents when using proxy -
running in windows.
the following code works fine. don't have specify username , password when setting proxy.
$acontext = array( 'http' => array( 'proxy' => 'tcp://ip:port', 'request_fulluri' => true, ), ); $cxcontext = stream_context_create($acontext); echo file_get_contents("someurl", false, $cxcontext);
however when try code won't work unless specify username , password proxy.
$ch = curl_init(); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_protocols, curlproto_http | curlproto_https); curl_setopt($ch, curlopt_proxy, 'http://ip:port'); curl_setopt($ch, curlopt_url, "someurl"); $responsebody = curl_exec($ch);
i http 407 error (received http code 407 proxy after connect) unless specify http://domain\user:password@ip:port
any ideas how make curl work without specifying user , password (like file_get_contents does)?
you can use curlopt_proxy
, curlopt_proxyport
options:
curl_setopt($ch, curlopt_proxy, "123.123.123.123"); curl_setopt($ch, curlopt_proxyport, 8080);
it's unclear question, if need use username , password authenticate proxy, can use curlopt_proxyuserpwd
option specify username , password , curlopt_proxyauth
option specify authentication method:
curl_setopt($ch, curlopt_proxyuserpwd, "username:password"); curl_setopt($ch, curlopt_proxyauth, curlauth_basic); //http basic auth
also, depending on type of proxy working with, may need specify type in curlopt_proxy
setting or separately via curlopt_proxytype
.
documentation:
Comments
Post a Comment