Curl has the best proxy support among many HTTP clients and download tools. This is how you use a socks5 proxy and also resolve hostname in the URL using the socks5 proxy. For some use case, resolving hostname via the proxy is essential. Suppose you have a socks5 proxy running on localhost:8001. In curl = 7.21.7, you can use. If you have php = 5.3 you could use autoprepend ini setting to include a script that uses streamcontextsetdefault to set a default proxy. But this will only set a proxy for the parts of php which use the default context, didnt test it but i think curl fore example doesent.
Curl is a utility used for data transfer in command lines or scripts. In this tutorial, you will learn how to use the curl command to connect via a proxy server on the Linux system.
Using Environment Variable
In a very simple word, you can simply set the following environment variables on your system. The curl command will automatically use these variables as a proxy. You can export those variables in your shell, like:
After exporting the environment variables, the curl command will automatically use the above proxy during a remote connection.
Once your work is done, you can simply unset these environment variables like:
Without Environment Variable
Instead of setting environment variables, you can simply pass the proxy details to curl command as a command-line parameter.
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.
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:
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:
After setting a proxy server, you will be able to perform the request using the curl_exec function. I.e.
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.
Php Curl Proxy
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.
Other authentication methods include the following:
- CURLAUTH_BASIC
- CURLAUTH_DIGEST
- CURLAUTH_GSSNEGOTIATE
- CURLAUTH_NTLM
- CURLAUTH_ANY
- CURLAUTH_ANYSAFE
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:
Php Curl Proxy Example
You can use the following API endpoints for testing purposes: