Change Ip when making a Curl request in php

Asked

Viewed 150 times

-1

Hello, all right?

I have a function that it receives a search term, goes google and returns a specific point of the page in this search term.

Everything working cute, except for the fact that at a certain time google recognizes with abnormal activity and no longer allows access.

Doing a search saw that it is possible to use curlopt_proxy to change the query ip by forging a tcp. But when including curlopt_proxy in my code the return is totally blank. I don’t know what happened.

    $Ch = curl_init();
    $timeout = 0;
    curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/search?client=safari&rls=x64&q='.$Busca.'&Num=1');
    curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
    curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $conteudo = curl_exec ($ch);
    curl_close($ch);

1 answer

2

The CURLOPT_PROXY sets the IP of the proxy you want to use. The proxy can be http (pattern), socks4 or socks5.

This is indicated in the documentation: https://curl.se/libcurl/c/CURLOPT_PROXY.html.


You cannot set any IP arbitrarily, after all, you want to also get the answer. Technically, you can forge Ips on TCP, but you won’t be able to get any results. Anyway, the CURLOPT_PROXY does not have that purpose, the CURLOPT_PROXY nay forge/change TCP packets to another arbitrary IP.

The CURLOPT_PROXY establishes a connection to a proxy server, it will forward your request. At the end, the final server (Google) will see the proxy IP (and will not see your real IP).


You must purchase proxies (search Google for "buy proxies"). There are several different types of proxies, you should find one that best fits your use case. Alternatively, you can create your own proxy as long as you have multiple Ips... Just set up a proxy server (like Squid Proxy Server) and then it will switch between the Ips you have. But again, you need to acquire Ips, and those Ips will be the ones that Google will see. The difference is, you can have a single server, and multiple outgoing Ips.

You can also try using free proxies (search for "free proxies") and get something like http://www.freeproxylists.net. Or, you can also choose to use Tor or I2P.

To test, you can request https://api.myip.com (instead of google) to see your IP. If the request via Curl has a different IP than your device, then the proxy is configured correctly.

Obviously, public (and free) proxies are used by anyone and may not solve your problem. After all, other people are using them to request Google.


Once you have the proxy IP you want to use specify it in CURLOPT_PROXY. Also, remove the CURLOPT_PROXYTYPE, or define it properly.

To see what error is occurring with your request, add a echo curl_error($ch).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.