How to use proxy with curl php

Using proxies with PHP cURL is a relatively straightforward process. Here's a step-by-step tutorial:

  1. Start by initializing your cURL handle using the curl_init() function:
    
    $ch = curl_init();
            
  2. Next, set the proxy options using the CURLOPT_PROXY and CURLOPT_PROXYPORT options. CURLOPT_PROXY sets the IP address or hostname of the proxy server, while CURLOPT_PROXYPORT sets the port number:
    
    curl_setopt($ch, CURLOPT_PROXY, 'proxy.example.com');
    curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
            
  3. If the proxy server requires authentication, you can set the username and password using the CURLOPT_PROXYUSERPWD option:
    
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'username:password');
            
  4. Set the URL of the page you want to fetch using the CURLOPT_URL option:
    
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/');
            
  5. Finally, execute the cURL request using curl_exec():
    
    $result = curl_exec($ch);
            
    And that's it! Here's the full code for reference:
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_PROXY, 'proxy.example.com');
    curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'username:password');
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/');
    $result = curl_exec($ch);
    curl_close($ch);