How to mention ip port in random ip curl script - php

I have a random IP script for curl but i don't know how to use port for same random script
function curl_get($kix)
{
$ips = array(
'85.10.230.132',
'88.198.242.9',
'88.198.242.10',
'88.198.242.11',
'88.198.242.12',
'88.198.242.13',
'88.198.242.14',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $kix);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_INTERFACE, $ips[rand(0, count($ips)-1)]);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
You can see here i have mentioned the ip but i dont know how to mention port for those ip's.
Thanks in advance !

If you have different ports per IP, you can associate the port to the IP with an associative array.
So for the following function I assume that you use PHP >= 7.0 and the following as format of our array.
array('IP'=>'Port');
We also CURLOPT_PORT to assign the alternative IP to connect with.
To quote the documentation:
CURLOPT_PORT: An alternative port number to connect to.
So back to your issue:
I use curl_setopt($ch, CURLOPT_PORT, $port); to assign the port we need to use.
I use array_rand($ips) to get a random key from the newly created associative array (The list of IP)
Finally I get its port with direct access to the array using $port = $ips[$randomIP];
So your function now looks like:
function curl_get($kix)
{
$ips = array(
'85.10.230.132' => '80',
'88.198.242.9' => '8080',
'88.198.242.10' => '5754',
'88.198.242.11' => '80',
'88.198.242.12' => '8888',
'88.198.242.13' => '8989',
'88.198.242.14' => '8080',
);
// We get a random key (IP)
$randomIP = array_rand($ips);
// We get the port according to the random key (IP)
$port = $ips[$randomIP];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $kix);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_INTERFACE, $randomIP);
// We set the alternative port with the following
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}

Related

Request-URI Too Long - SMS API

My problem is somehow peculiar. I have this bulksms api from my provider:
http://www.estoresms.com/smsapi.php?username=user&password=1234&sender=##sender##&recipient=##recipient##&m
essage=##message##&
then i wrapped it in PHP and passed it in cURL:
$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers."&message=".$text."&";
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$send_it = curl_get_contents($api);
ordinarily, it worked fine, but when $recepient (phone numbers) are more than say 300, i get an error:
Request-URI Too Long
The requested URL's length exceeds the capacity limit for this server.
Additionally, a 414 Request-URI Too Long error was encountered while trying to use an ErrorDocument to handle the request.
But BulkSMS should be able to send to thousands of numbers at a time.
From my research, i found out that there's a limit to URL. I'm not the server owner. i working on a shared hosting plan. pls how can i get around this problem. I know there's a solution to it that would not mean buying my own server.
Thanks
Can you try to make the API use POST instead of GET. It would solve the issue.
Edit:
I'm not sure your API check POST, but try that:
$api = "http://www.estoresms.com/smsapi.php";
$data = array('username' => $sms_user, 'password' => $sms_pwd, 'sender' => $sender_id , 'recipient' => $numbers , 'message' => $text);
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$send_it = curl_get_contents($api);
Have a look at this code example (from bulksms.com).
http://developer.bulksms.com/eapi/code-samples/php/send_sms/
So then, i had to find a way around my own problem. if the API will not allow thousands of numbers at a time, then let's break it into chunks at the point of execution.
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$how_many = count(explode(',', $numbers));
if ($how_many > 250){
$swi = range(0, ceil($how_many/250)-1);
foreach ($swi as $sw){$numbers_a = implode(',', (array_slice(explode(',', $numbers), $sw*250, 250)));
$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers_a."&message=".$text."&";
$send_it = curl_get_contents($api);
}
}
if ($how_many <= 250){
$api = "http://www.estoresms.com/smsapi.php?username=".$sms_user."&password=".$sms_pwd."&sender=".$sender_id."&recipient=".$numbers."&message=".$text."&";
$send_it = curl_get_contents($api);
}

How do I cURL to this type of URL in PHP [192.168.1.30:8080/server]?

I know you can specify the port number using CURLOPT_PORT, however the server is hosted on the directory stated after the port number. If cURL cannot accomplish this, is there a method which allows me to POST form data to the URL format I specified above? Please help.
Yes, you just need specify CURLOPT_PORT and omit port in your url, like:
<?php
$post = [
'field' => 'value',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://192.168.1.30/server');
curl_setopt($ch,CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException(curl_error($ch));
}
var_export($response);

PHP set random variable from proxy list and Curl, if fail repeat

I am trying to figure out how to randomly select a proxy ip from a list and then performing a curl with it, and if a fail occurs use a new proxy ip. Here is my working code without the randomization:
$url = "www.example.com";
$loginpassw = 'myproxypw';
$proxy_ip = '23.27.37.128';
$proxy_port = '29842';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
$html = curl_exec($ch);
if (strpos($html,'To continue, please type the characters below') !== false) {
echo "now an error has occurred, let's try a new proxy";
}
curl_close($ch);
Ideally the proxy_ip and proxy_port must stay the same in a list of say:
$proxylist = array (
array("ip" => "23.27.37.128", "port" => "29842"),
array("ip" => "23.27.37.111", "port" => "29852"),
array("ip" => "23.27.37.112", "port" => "29742"),
array("ip" => "23.27.37.151", "port" => "29242")
);
I was wondering if I could possibly use shuffle:
shuffle($proxylist);
while($element = array_pop($proxylist)){
return $element;
}
My second question would be the best way of doing this, my PHP is not perfect so I am wondering rather than rewriting the top curl over and over should I store it inside a function?
Any help appreciated.
Thanks,
Simon
Edit:
The following code seems to be working where I have split my code into two functions:
function curltime($url, $proxy_ip, $proxy_port, $loginpassw){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
return curl_exec($ch);
curl_close($ch);
}
//now let's do the curl
$url = "www.example.com";
$proxylist = array (
array("proxyip" => "23.27.37.128", "proxyport" => "29842"),
array("proxyip" => "23.27.37.111", "proxyport" => "29852"),
array("proxyip" => "23.27.37.112", "proxyport" => "29742"),
array("proxyip" => "23.27.37.151", "proxyport" => "29242")
);
foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
$$key = $value;
}
$html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');
if (strpos($html,'To continue, please type the characters below') !== false) {
echo "now we have errors so let's try again"
foreach ($proxylist[mt_rand(0,count($proxylist)-1)] as $key => $value) {
$$key = $value;
}
$html = $this->curltime($url, $proxyip, $proxyport, 'somepassword');
}
$cache .= $html;
Anyone know of a better way for me to do the looping?
To get a random proxy from the list you could use this:
$proxylist[mt_rand(0,count($proxylist)-1)]
Explained:
count($array) Get length of array
mt_rand($x,$y) Get a random number between $x and $y
Edit:
It is totaly possible to do like you did also. Then just always take like the first element of the array.
shuffle($array);
$array[0]
Which of these two options are best for the randomness I can't really say though.

Specify source port range with curl using PHP

I need to specify the source port range for curl.
I don't see any option who let me choose a range for source port in TCP.
Is it possible?
You can use CURLOPT_LOCALPORT and CURLOPT_LOCALPORTRANGE options, which are similar to curl's --local-port command line option.
In the following example curl will try to use source port in range 6000-7000:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_LOCALPORT, 6000);
curl_setopt($ch, CURLOPT_LOCALPORTRANGE, 1000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
From the command line one would use:
curl --local-port 6000-7000 <url>
For documentation see: CURLOPT_LOCALPORT and Local port number.
I think it would be better using fsockopen. I came up many times this worked for me when blocked by firewalls. See: http://php.net/fsockopen
$ports = array(80, 81);
foreach ($ports as $port) {
$fp =# fsockopen("tcp://127.0.0.1", $port);
// or fsockopen("www.google.com", $port);
if ($fp) {
print "Port $port is open.\n";
fclose($fp);
} else {
print "Port $port is not open.\n";
}
}
By the way, there is CURLOPT_PORT for CURL, but doesn't work with tcp://127.0.0.1;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1");
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$re = curl_exec($ch);
// echo curl_errno($ch);
curl_close($ch);
print $re;

SSL Issue, Logging in to website

I have this code to login to a server called Pinger TextFree for a bot I'm working on:
<?php
function sendRequest($url, $postorget, $fields = array(), $proxy)
{
$cookie_file = "cookies.txt";
//Initiate connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // set url
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url); // set url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the transfer
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // allow https
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); // random agent
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // automatically follow Location: headers (ie redirects)
curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // auto set the referer in the event of a redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); // ibm likes to redirect a lot, make sure we dont get stuck in a loop
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // file to save cookies in
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // file to read cookies from
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40); //timeout time for curl
curl_setopt($ch, CURLOPT_PORT, 80); //port to connect to (default 80 obviously)
//Check to see if a proxy is being used
if(isset($proxy)){
//Tell cURL you're using a proxy
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//Set the proxy
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
//Check if request is POST or GET
if ($postorget == "post" OR $postorget == "POST")
{
curl_setopt($ch, CURLOPT_POST, true); // use POST
if (is_array($fields)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); // key => name gets turned into &key=name
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // &key=name passed in
}
} else {
curl_setopt($ch, CURLOPT_POST, false); // use GET
}
$content = curl_exec($ch); // return html content
$info = curl_getinfo($ch); // return transfer info
$error = curl_error($ch); // return any errors
curl_close($ch);
$request = array('content' => $content,
'error' => $error,
'info' => $info);
return $request;
}
//Login details
$username = "usernamehere";
$password = "passwordhere";
//GET the initial login page
$initFields = "";
$initOutput = sendRequest("http://www.pinger.com/tfw/?t=1360619019053", "GET", $initFields);
echo "<textarea cols='100' rows='400'>";
print_r($initOutput);
echo "</textarea>";
//Login to pinger
$loginFields = "{\"username\":\"".$username."\",\"password\":\"".$password."\",\"clientId\":\"textfree-in-flash-web-free-1360619009-8CA1C5C1-38ED-2E31-3248-CB367450A20F\"}";
$loginOutput = sendRequest("https://api.pinger.com/1.0/web/login", "POST", $loginFields);
echo "<textarea cols='100' rows='400'>";
print_r($loginOutput);
echo "</textarea>";
?>
For some reason every time I try to run this script all I get is "Unknown SSL protocol error in connection to api.pinger.com:80"
What am I doing wrong here? I'll even specify SSL2 in the setop but it just hangs forever - I just can't get this to work!
Here is the app I'm trying to automate: http://www.pinger.com/tfw/
It's in flash but I'm using Fiddler to sniff the HTTP/HTTPS requests to automate them with cURL.
Any ideas from you guys?
The error is obviously in the line
curl_setopt($ch, CURLOPT_PORT, 80); //port to connect to (default 80 obviously)
HTTPS servers listen on port 443 by default. Simply deleting this line should be sufficient; curl will then figure out the port from the protocol in the URL.

Categories