user5247236
user5247236

Reputation:

How to use proxy in php curl

I have a curl function:

function curl($url, $referer, $type=null){
    $agent = ($type != null && $type = 'movil') ? 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' : 'Mozilla/5.0(Windows;U;WindowsNT5.0;en-US;rv:1.4)Gecko/20030624Netscape/7.1(ax)';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $page = curl_exec($ch);
    curl_close($ch);
    return $page;
}

But this function displays my server ip on request so would be great to know any way to use it via proxy ip and port.Thankyou

Upvotes: 0

Views: 12346

Answers (1)

Raj Vimal Chopra
Raj Vimal Chopra

Reputation: 56

If the purpose is to hide the source IP (request originating server's IP), then with curl it is not possible as it is a low-level operation which requires manipulation of raw socket connections.

If you are looking to use proxy while making a request to some URL, then you have to use "CURLOPT_PROXY".

function curl($url, $referer, $type=null){
    $agent = ($type != null && $type = 'movil') ? 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' : 'Mozilla/5.0(Windows;U;WindowsNT5.0;en-US;rv:1.4)Gecko/20030624Netscape/7.1(ax)';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_PROXY, '8.8.8.8'); // replace 
8.8.8.8' with proxy server's IP.
    curl_setopt($ch, CURLOPT_PROXYPORT, '2211'); // replace '2211' with Proxy server's port.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_REFERER, $referer);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $page = curl_exec($ch);
    curl_close($ch);
    return $page;
}

Upvotes: 1

Related Questions