In PHP, cURL is a powerful library for performing various HTTP requests. In many cases, we need to initiate a request through a proxy server, such as to hide the real IP or access restricted resources. This article will explain in detail how to set up a proxy server in conjunction with curl_setopt function when closing a cURL session using curl_close to implement request proxy.
The general process of sending requests using cURL is as follows:
Initialize the session: curl_init()
Set request options: curl_setopt()
Execute request: curl_exec()
Close the session: curl_close()
The focus of this article is on how to set up a proxy server in step 2.
cURL provides special options to set up the proxy:
CURLOPT_PROXY : proxy server address (IP or domain name and port)
CURLOPT_PROXYPORT : proxy server port (optional)
CURLOPT_PROXYUSERPWD : The username and password of the proxy server, the format is username:password (optional)
CURLOPT_PROXYTYPE : Proxy type, default is HTTP proxy, common types include SOCKS5, etc.
Sample code:
<?php
// initialization cURL Session
$ch = curl_init();
// Set up a request URL,Here is a sample interface,Replace the domain name with gitbox.net
curl_setopt($ch, CURLOPT_URL, "https://api.gitbox.net/example/data");
// Returns the response instead of direct output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the proxy server address and port
curl_setopt($ch, CURLOPT_PROXY, "proxy.gitbox.net"); // Proxy server address
curl_setopt($ch, CURLOPT_PROXYPORT, 8080); // Proxy port
// If the agent needs authentication,Set username and password
// curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:password");
// If needed SOCKS5 acting,设置acting类型
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
// Execute a request
$response = curl_exec($ch);
// Determine whether the request is successful
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
} else {
echo $response;
}
// closure cURL Session
curl_close($ch);
Options | illustrate |
---|---|
CURLOPT_PROXY | Set the proxy server address, support IP and domain name |
CURLOPT_PROXYPORT | Set the proxy server port, default is 8080 |
CURLOPT_PROXYUSERPWD | Set the username and password for proxy authentication, the format is user:pass |
CURLOPT_PROXYTYPE | Set proxy types, such as HTTP (default), SOCKS4, SOCKS5, etc. |
CURLOPT_HTTPPROXYTUNNEL | Whether to enable HTTP proxy tunnel, default to false |
For example, if the proxy server requires username and password authentication and is a SOCKS5 proxy, you can write it like this:
curl_setopt($ch, CURLOPT_PROXY, "proxy.gitbox.net");
curl_setopt($ch, CURLOPT_PROXYPORT, 1080);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:password");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.gitbox.net/example/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "proxy.gitbox.net");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
$response = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
} else {
echo $response;
}
curl_close($ch);
Use the CURLOPT_PROXY series option of curl_setopt to set up a proxy server easily.
The proxy address should state the domain name or IP, and the port can be set separately.
When authentication is required, provide the username and password through CURLOPT_PROXYUSERPWD .
The proxy type is set according to requirements. HTTP is the default type and needs to be specified specifically when a SOCKS5 proxy is specified.
After use, call curl_close to release the resource.
In this way, you can flexibly use cURL in PHP to cooperate with the proxy server to initiate HTTP requests to meet various network environment needs.