Proxy using curl php

How to have PHP to use Proxy Setting to connect to internet?

I’m behind a proxy server that does not allow a direct connection to internet. All of my PHP applications fail to connect to internet for their update checks and etc. How can I tell PHP my Proxy Settings? I don’t want to enter proxy settings into the code, I want PHP itself use it via a global config setting or something similar.

8 Answers 8

if almost all of you internet access need a proxy, I’d prefer do like this.

//add this as the first line of the entry file may it is the index.php or config.php stream_context_set_default(['http'=>['proxy'=>'proxy-host:proxy-port']]); 

the proxy will work for file_get_contents but not curl_exec

sorry for my mistake, the stream api is not affect the curl, if you use curl, you’d better set it by curl_setopt($handle, CURLOPT_PROXY, $proxy_url); , and if you use both, you can add both two.

@Tarik if your proxy server need a basic authentication, your need to do it like this stream_context_set_default([‘http’=>[‘proxy’=>’proxy-host:proxy-port’, ‘header’=>’Proxy-Authorization: Basic ‘.base64_encode(‘your-username:your-password’)]]);

@Tarik the key point is the Proxy-Authorization header, you can find more information at @pascal-martin ‘s answer

@IanHu Thanks, I found my solution with this code: ‘$curlProxy = array( ‘http’=>array( ‘method’=>»GET», ‘header’=>»Proxy-Authorization: Basic AUTH_CODE_HERE==\r\n» . «Proxy-Connection: Keep-Alive», ‘proxy’=>»IP_ADDRESS:PORT» ) );’

This depends on how your PHP application connects to the internet.

If taking the most likely situation using PHP cUrl. In that case the following options will help you:

curl_setopt($handle, CURLOPT_PROXY, $proxy_url); curl_setopt($handle, CURLOPT_PROXYUSERPWD, "[username]:[password]"); 

thanks for the answer. but consider that this may be applied just for my own codes. what about other PHP applications like Drupal? I need PHP itself connect through Proxy not via the code.

function getUrl($url) < $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com"); //your proxy url curl_setopt($ch, CURLOPT_PROXYPORT, "8080"); // your proxy port number curl_setopt($ch, CURLOPT_PROXYUSERPWD, "username:pass"); //username:pass $file_contents = curl_exec($ch); curl_close($ch); return $file_contents; >echo getUrl("http://www.google.com"); 

You can configure stream_context_set_default in a file, and include this file in all your Php program, by using auto_prepend_file php.ini property.

I wrote a small gist about:

This technique is «cool» because, this allows your sys-admin to configure the host, so developer don’t have to change anything in the code.

I searched over the internet and could not find anything about stream_context_set_default() with a password protected proxy server.

Читайте также:  Java iterator remove all elements

Then I thought that password in basic authorization is sent in headers. So I modified the headers with the password extracted from a CURL request and it worked perfect.

Here is how you do it:

First send this request to any domain (example.com) as below:

curl -v -U user:pass -x your_proxy_ip:port --url https://example.com 

See the curl sent headers and I got these proxy lines to use later:

> Trying XXX.XXX.XXX.XXX. > Connected to XXX.XXX.XXX.XXX (XXX.XXX.XXX.XXX) port YYYY (#0) > Establish HTTP proxy tunnel to example.com:443 > Proxy auth using Basic with user 'your_username_here' > CONNECT example.com:443 HTTP/1.1 > Host: example.com:443 > Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg > User-Agent: curl/7.47.0 > Proxy-Connection: Keep-Alive > < HTTP/1.1 200 Connection established < < Proxy replied OK to CONNECT request 

OK now it is time to build our custom header:

$default_opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Proxy-Authorization: Basic YW1hem9uOnXXXXXXXXXXXXXXX25SMg\r\n" . "Proxy-Connection: Keep-Alive", 'proxy'=>"XXX.XXX.XXX.XXX:YYYY" ) ); $default = stream_context_set_default($default_opts); $result = file_get_contents("https://ipleak.net/json/"); print_r(json_decode($result)); 

And it works perfect, you get the IP of your proxy server in response!

Источник

PHP: Using cURL with a proxy.

This is a guide on how to use a proxy with PHP’s cURL functions. In this tutorial, we will send our HTTP request via a specific proxy IP and port.

Why use a proxy?

There are various reasons why you might want to use a proxy with cURL:

  1. To get around regional filters and country blocks.
  2. Using a proxy IP allows you to mask your own IP address.
  3. To debug network connection issues.

Using a proxy with PHP’s cURL functions.

Take a look at the following PHP code, which you can use to authenticate with a proxy via cURL and send a HTTP GET request.

//The URL you want to send a cURL proxy request to. $url = 'http://php.net'; //The IP address of the proxy you want to send //your request through. $proxyIP = '1.2.3.4'; //The port that the proxy is listening on. $proxyPort = '1129'; //The username for authenticating with the proxy. $proxyUsername = 'myusername'; //The password for authenticating with the proxy. $proxyPassword = 'mypassword'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL , 1); //Set the proxy IP. curl_setopt($ch, CURLOPT_PROXY, $proxyIP); //Set the port. curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort); //Specify the username and password. curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$proxyUsername:$proxyPassword"); //Execute the request. $output = curl_exec($ch); //Check for errors. if(curl_errno($ch)) < throw new Exception(curl_error($ch)); >//Print the output. echo $output;

In the code snippet above, we connected to a proxy that requires authentication before sending a simple GET request.

If the proxy in question does not require authentication, then you can omit the CURLOPT_PROXYUSERPWD line from your code.

“Failed to connect to 1.2.3.4 port 1129: Timed out”

This means that cURL could not connect to the proxy on that IP and port. Make sure that both the IP and port are correct and that the proxy is operating correctly.

“Failed to connect to 1.2.3.4 port 1129: Connection refused”

This error usually occurs when you have specified an incorrect port number. i.e. The IP address of the proxy was correct, but it is not listening for requests on that port. There is also the possibility that the server is up, but the software that runs the proxy is not running.

Читайте также:  Web application development with yii 2 and php

“Received HTTP code 407 from proxy after CONNECT”

The username and password combination that you are using with CURLOPT_PROXYUSERPWD is incorrect. Make sure that you are separating the username and password by a colon : character.

Источник

Using proxy servers with cURL in PHP

d

Setting a proxy server to be used with cURL and PHP is relatively simple, it mostly depends on the server that you are using, and authentication method (if any). The HTTP authentication method is controlled with the CURLOPT_PROXYAUTH option, the default method is CURLAUTH_BASIC – if the proxy requires authentication, a username and password can be set in the [username]:[password] format, using the CURLOPT_PROXYUSERPWD option.

Note. instead of using proxy servers, it can be better to use a VPN connection inside a virtual machine or a Docker container, because it will be separated from your main OS, and the connection might be faster and more stable.

Using public proxy servers is not recommended, and it might even be illegal – you should examine this carefully before using any proxy servers that you might find. In addition, proxy servers – perhaps especially public ones – could be under the control of hackers that are using them to spy on the traffic going through the server. You should be very careful about using public proxy servers for these reasons.

For now we'll just focus on using a proxy that doesn't require any authentication. Setting a proxy server and a port number in PHP for cURL can be done using the CURLOPT_PROXY option, like shown in the below example:

curl_setopt($ch, CURLOPT_PROXY, '128.0.0.3:8080'); 

As shown in the above example, you can set the a proxy with the IP:PORT syntax in PHP using cURL. But if you prefer to keep the ip seperated from the port, you can also use the CURLOPT_PROXYPORT option, which would result in the below PHP code:

curl_setopt($ch, CURLOPT_PROXY, '128.0.0.3'); curl_setopt($ch, CURLOPT_PROXYPORT, '8080'); 

After setting a proxy server, you will be able to perform the request using the curl_exec function. I.e.

$ch = curl_init($url); $url = "http://beamtic.com/Examples/http-post.php"; curl_setopt($ch, CURLOPT_PROXY, '128.0.0.3'); curl_setopt($ch, CURLOPT_PROXYPORT, '8080'); // Perform the request, and save content to $result $result = curl_exec($ch); echo $result; 

Setting cURL Proxy Type

cURL supports two proxy types, the default is HTTP, and the other option is SOCKS5. You can set the proxy type using the CURLOPT_PROXYTYPE option. I.e.

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 

You really only need to set the type of the proxy, if you are not using a HTTP proxy.

Setting Authentication Method

As mentioned in the beginning of the tutorial, setting the authentication method of a proxy server can be done using the CURLOPT_HTTPAUTH option. To make this work properly, we will also need to provide a username and password for the proxy server, this is all accomplished in the below script, in which we are just using a BASIC authentication method.

$ch = curl_init($url); $url = "http://beamtic.com/Examples/http-post.php"; curl_setopt($ch, CURLOPT_PROXY, '128.0.0.3'); curl_setopt($ch, CURLOPT_PROXYPORT, '8080'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // The username and password curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'USERNAME:PASSWORD'); // Perform the request, and save content to $result $result = curl_exec($ch); echo $result; 

Other authentication methods include the following:

  • CURLAUTH_BASIC
  • CURLAUTH_DIGEST
  • CURLAUTH_GSSNEGOTIATE
  • CURLAUTH_NTLM
  • CURLAUTH_ANY
  • CURLAUTH_ANYSAFE
Читайте также:  Linkedlist java сложность операций

Note.The vertical bar | (or) operator can be used to combine methods. If this is done, cURL will poll the server to see what methods it supports and pick the best.

Tools:

You can use the following API endpoints for testing purposes:

https://beamtic.com/api/user-agent
https://beamtic.com/api/request-headers

Источник

How to use CURL via a proxy?

I am looking to set curl to use a proxy server. The url is provided by an html form, which has not been a problem. Without the proxy it works fine. I have found code on this and other sites, but they do not work. Any help in finding the correct solution would be much appreciated. I feel that the bellow are close, but that I am missing something. Thank You. The bellow code I adapted from here http://www.webmasterworld.com/forum88/10572.htm but it returns an error message about a missing T_VARIABLE on line 12.

is currently live on pelican-cement.com but also does not work. UPDATE: Thank you for all your help, I made the above changes. Now it only returns a blank screen.

also, you need to change $url = '$_POST[1]' to $url = $_POST[1] - otherwise, $url will be a string instead of the URL you want

@user586011: Please add your solution as an answer below and accept it. Don't put the solution into the question, that does not work well.

4 Answers 4

Here is a working version with your bugs removed.

$url = 'http://dynupdate.no-ip.com/ip.php'; $proxy = '127.0.0.1:8888'; //$proxyauth = 'user:password'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_PROXY, $proxy); //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $curl_scraped_page = curl_exec($ch); curl_close($ch); echo $curl_scraped_page; 

I have added CURLOPT_PROXYUSERPWD in case any of your proxies require a user name and password. I set CURLOPT_RETURNTRANSFER to 1, so that the data will be returned to $curl_scraped_page variable.

I removed a second extra curl_exec($ch); which would stop the variable being returned. I consolidated your proxy IP and port into one setting.

I also removed CURLOPT_HTTPPROXYTUNNEL and CURLOPT_CUSTOMREQUEST as it was the default.

If you don't want the headers returned, comment out CURLOPT_HEADER .

To disable the proxy simply set it to null.

curl_setopt($ch, CURLOPT_PROXY, null); 

Any questions feel free to ask, I work with cURL every day.

Источник

Оцените статью