0

Running in Windows.

The following code works fine. I don't have specify a username and password when setting up the 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 I try this code it won't work unless I specify the username and password for the 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 get an HTTP 407 error (Received HTTP code 407 from proxy after CONNECT) unless i specify the http://domain\user:password@ip:port

Any ideas how to make cURL work without specifying the user and password (like file_get_contents does)?

2
  • WHen using CURLOPT_PROXY try splitting the IP and Port section. curl_setopt($ch, CURLOPT_PROXY, 'X.X.X.X'); curl_setopt($ch, CURLOPT_PROXYPORT, 'XXXX'); Commented Jun 18, 2015 at 21:00
  • @Bijan same error when specifying url and port separately. just tried it. Commented Jun 18, 2015 at 21:01

1 Answer 1

2

You can use the CURLOPT_PROXY and CURLOPT_PROXYPORT options:

curl_setopt($ch, CURLOPT_PROXY, "123.123.123.123");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);

It's unclear from your question, but if you need to use a username and password to authenticate with this proxy, then you can use the CURLOPT_PROXYUSERPWD option to specify your username and password and the CURLOPT_PROXYAUTH option to specify your authentication method:

curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:password");
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //HTTP Basic auth

Also, depending on what type of proxy you are working with, you may need to specify the type in the CURLOPT_PROXY setting or separately via CURLOPT_PROXYTYPE.

Documentation:

http://php.net/manual/en/function.curl-setopt.php

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

Sign up to request clarification or add additional context in comments.

5 Comments

Just tried this with IP and port specified separately and receive the same error. curl_setopt($ch, CURLOPT_PROXY, 'x.x.x.x'); curl_setopt($ch, CURLOPT_PROXYPORT, '8080');
not 100% sure. The IT department has set it up (will try to find out). I know it is tracking the user who uses the proxy (and what I am doing is okay to do - just prefer to not have to hardcode a password in cURL). Almost feels like file_get_contents is pulling user credentials from Windows and passing them along.
"I know it is tracking the user who uses the proxy (and what I am doing is okay to do - just prefer to not have to hardcode a password in cURL). " -- I'm confused -- do you need to use a username/password to access the proxy or does it not require a username and password?
Using file_get_contents I do not have to specify a username/password. Using cURL it seems I do (so far). I am somewhat confused as well. Will try to find out more from the network folks and respond.
maybe the proxy's checking UA's and is allowing whatever ua that f_g_c uses, and requires a password for curl's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.