curl with a random proxy – Stack Overflow
maybe the problem is with your loadProxies(5) return? cus this code works fine right here right now:
php
$proxies = array('86. 188. 142. 244:8080'); // random public proxy
function getData($proxylist)
{
$rand_proxy = rand(0, count($proxylist)-1);
$url = ''; //just for example
$agent = "Mozilla/5. 0 (X11; U; Linux i686; en-US) AppleWebKit/532. 4 (KHTML, like Gecko) Chrome/4. 0. 233. 0 Safari/532. 4";
$referer = ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_PROXY, $proxylist[$rand_proxy]);
$data = curl_exec($ch);
if($data! ==true){$ex=new RuntimeException('curl_exec error. errno: '. curl_errno($ch). ' error: '. curl_error($ch));@curl_close($ch);throw $ex;}
curl_close($ch);
//echo $data;}
getData($proxies);
also, $data just returns the return code for curl, not the data, because, by default, curl outputs the response body to stdout. if you set CURLOPT_RETURNTRANSFER, it will return the body. (to redirect it to something else, use CURLOPT_FILE)
Random Proxy in PHP [Archive] – Dynamic Drive Forums
View Full Version: Random Proxy in PHP
Vaonline09-16-2012, 11:55 PMHello.
I am looking for a PHP code that selects a random proxy between almost 400 proxys, and it intercepts in a webpage.
Lets say that if i put as the url of the page that i want the php to intercept, it must show the ip of the proxy.
I’ve heard that the function must be curl but i dont know what does this means…
Can someone help me?
Thanks.
djr3309-17-2012, 12:07 AMWhat? I really don’t follow how that would work. When you say “put the URL” you mean in a form, or in the address bar?
I think you may be looking at proxies backwards. You go to the proxy then enter the URL you want to go to, rather than the other way around of going to the site and then using a proxy.
Look at for more info on curl if you want to try that.
It’s not particularly easy, but it could work.
Alternatively if you have remote files enabled in your PHP configuration you could just use include() to bring in the content of another page.
But running a proxy through your server actually means you’d have two levels of proxy– one is your site and the other is the proxy.
Proxies are usually useful for either:
1) Viewing blocked websites from an internet connection.
2) Hiding your actual browsing activity from anyone who may be watching (on the network or at the other end).
(Both of which may be used for good or evil, I suppose. )
I’m not sure how your plan will help with either (1) or (2).
Secondly, if you use your server as the hub for all of this, then I imagine that proxy sites will start blocking its IP very quickly due to overuse. Every person who visits your site will be going through your server’s IP and then that IP will be requesting every document from these proxies. Many of them have limits per user (which in this case would be identical for all of your users– your server’s IP). You’re creating a bottleneck that is a little counterproductive, as I see it.
A simple alternative to all of this is to have a form with a URL input and a submit button that takes you to a random proxy when you click submit. That’s just a redirect and would be much easier and in the end much more effective. But sites already exist like that, so I don’t know if you’d gain much by doing it.
Vaonline09-17-2012, 03:50 AMHello, thank you very much for your answer.
What i really want is to visit a same webpage with different proxy.
I dont want to click ‘submit’ or ‘go’ or any button, i want that when the page loads, it loads a webpage with a random proxy of the list that i have made in the php.
By saying ‘put url’ i mean in the php code.
An example could be this, but i prefer to forget this code because it does not works.
In the Bolds:
1. There is the list of the proxys.
2. The url of the webpage i want the php to intercept with a random proxy.
php
function get_random_proxy(){
srand ((double)microtime()*1000000);
$f_contents = file (");
$line = $f_contents[array_rand ($f_contents)];
return $line;}
function get_curl_proxy($url){
$proxy_ip = get_random_proxy();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;}
$u=";
echo get_curl_proxy($u);? >
So, how can i do this? As i said, this code does not work for me…
I am literally a noob in php… and i need this, please.
djr3309-17-2012, 04:36 AMI’m still entirely missing what the purpose of this is. Why build a proxy (that’s what you’re building! ) that uses other proxies for the information? I just don’t get it. And I think it will cause you to run into problems in the end with how it will work.
curl() is fine. So is include() if you allow remote files.
Your code seems fine (at least as an approach– I haven’t tested it).
By saying ‘put url’ i mean in the php you only want one URL then?
Now… to the extent that I do understand what you might be doing:
It looks like you may be attempting to violate the terms of service of various proxies by figuring out which IPs they use, and so forth, or possibly to attempt to circumvent either the IP detection website’s usage limits, or the proxies’ usage limits.
Any type of request/question that is a violation of the TOS of another website or potentially illegal is strictly not answered here.
Vaonline09-17-2012, 05:09 AM$u=”;
That is only to know if the PHP code is working, otherwise, if i dont link to a webpage to know my IP direction, how could i know if it the php proxy code is working?
So you only want one URL then?
Yes, only one url.
The purpose is to know the real stabilty of a stream server along with different countries visits, but i havent put the stream server in the file.
Could you please use the code and see what’s missing? I would be very grateful.
djr3309-17-2012, 05:16 AMThat is only to know if the PHP code is working, otherwise, if i dont link to a webpage to know my IP direction, how could i know if it the php proxy code is working? Ok, fair enough. But you could do this yourself quite easily in PHP and create a page on your own server. It would be easy and eliminate any possible problems with their limits on the number of requests you can make. You just need this code:
php echo $_SERVER['REMOTE_ADDR'];? >
(And call that or whatever you want; use that as your URL and it should work for you. )
The purpose is to know the real stabilty of a stream server along with different countries visits, but i havent put the stream server in the, here’s where it gets tricky. Are you testing media, beyond just HTML (text)? curl() is going to do much for you for any additional elements (even images) being loaded on the page. A full proxy can do this, but you’re just grabbing content and effectively pasting the HTML into your current document. It will work for HTML, but not beyond that.
You’ll have to truly build a proxy (probably not in PHP, at the server level instead– some kind of proxy server, although I don’t know the details) to make that really work if you need things like Flash, or even just images to work as well.
Vaonline09-17-2012, 05:42 AMOk, i have made the
Well, i put an embed code made of html, flash, etc. But i want to test how many views it can support or how many from which country, etc.
So what would i need to do in the php code?
Thanks for your answers.
djr3309-17-2012, 06:02 AMYou really can’t do this, at least not at your level of PHP (or honestly mine). You could do this another way, but PHP is not the right answer.
You’d need a full proxy server which means that any request is rerouted automatically to the other site.
What you’d need to do is rewrite the HTML as you go so that the link/href properties of all links and CSS files and Flash embeds and images and whatever else all go to your site. Then you would need to design your proxy to be able to handle requests for content other than html. That’s easy enough (well, somewhat difficult, but very possible) with certain file types like CSS or JS, or even images, because PHP has built in ways to do this. But beyond that, once you start doing something like video or plugins like Flash, PHP simply isn’t equipped to deal with it. I suppose you could in theory literally expand the capabilities of PHP, but that won’t be a practical solution.
Does that make sense?
So either look for some proxy server software to us as-is, or don’t approach it this way. Here are two alternatives:
1. Limit the type of files you may deal with, as just a test of speed. So use images (as an example) and maybe use images that are 1mb (very large, good to test for limits). Then run what you’re doing (which is still difficult, but possible).
2. Find some existing site testing software. That’s strongly my recommendation here. There are probably services out there that will do all of this for you and tell you how well your website holds up to stress and lots of traffic.
Powered by vBulletin® Version 4. 2. 2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.
Using proxy servers with cURL in PHP | Beamtic
Tutorial on how to use proxy servers with cURL and PHP
34141 views
By. Jacob
Edited: 2021-04-12 08:45
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:
curl_setopt($ch, CURLOPT_PROXY, ‘128. 0. 3:8080’);
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:
curl_setopt($ch, CURLOPT_PROXY, ‘128. 3’);
curl_setopt($ch, CURLOPT_PROXYPORT, ‘8080’);
After setting a proxy server, you will be able to perform the request using the curl_exec function. I. e.
$ch = curl_init($url);
$url = “;
// Perform the request, and save content to $result
$result = curl_exec($ch);
echo $result;
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. e.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
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.
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// The username and password
curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘USERNAME:PASSWORD’);
Other authentication methods include the following:
CURLAUTH_BASIC
CURLAUTH_DIGEST
CURLAUTH_GSSNEGOTIATE
CURLAUTH_NTLM
CURLAUTH_ANY
CURLAUTH_ANYSAFE
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:
Your User Agent
Your Request Headers
You can use the following API endpoints for testing purposes:
How to use the AVIF image format in PHP; A1 or AVIF is a new image format that offers better compression than WebP, JPEG and PNG, and that already works in Google Chrome. How to create a router in PHP to handle different request types, paths, and request parameters. How much faster is C++ than PHP to increment and display a counter in a loop? Detecting the request method used to fetch a web page with PHP, routing, HTTP responses, and more. How to create a custom error handler for PHP that handles non-fetal in: PHP Tutorials