Return online users curl - php

With this code can return the online users only on localhost. When sending to webhost crashes, even seeking the token code example in the second call. It only works with the second parameter tokem generated on site page source code. How to run this script from the webhost?
if (!function_exists('getHistats')) {
function getHistats($sid = 0, $cc = '') {
if (empty($sid) || empty($cc))
return 'error';
$url = 'http://www.histats.com/viewstats/HST_GET_SUMMARY.php';
$result = '';
$ualist = array(
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)',
'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.23 (KHTML, like Gecko) Ubuntu/10.04 Chromium/11.0.688.0 Chrome/11.0.688.0 Safari/534.23',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110221 Ubuntu/10.04 (lucid) Firefox/3.6.14 GTB7.1',
'Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.01',
'Midori/0.2.2 (X11; Linux i686; U; en-us) WebKit/531.2+',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1150.1 Iron/20.0.1150.1 Safari/536.11'
);
if (function_exists('curl_init')) {
$http_headers = array();
$http_headers[] = 'Host: www.histats.com';
$http_headers[] = 'Referer: www.histats.com/viewstats/?sid='. $sid .'&act=2&f=1';
$http_headers[] = 'X-Requested-With: XMLHttpRequest';
$opts = array();
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_HTTPHEADER] = $http_headers;
$opts[CURLOPT_CONNECTTIMEOUT] = 5;
$opts[CURLOPT_TIMEOUT] = 10;
$opts[CURLOPT_USERAGENT] = $ualist[rand(0, count($ualist) - 1)];
$opts[CURLOPT_HEADER] = FALSE;
$opts[CURLOPT_RETURNTRANSFER] = TRUE;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = 'AR_REQ[sid]='. $sid .'&AR_REQ[CC]='. $cc .'&dbg=1';
# Initialize PHP/CURL handle
$ch = curl_init();
curl_setopt_array($ch, $opts);
# Create return array
$result = curl_exec($ch);
curl_close($ch);
} elseif (ini_get('allow_url_fopen')) {
$result = file_get_contents($url);
}
if (empty($result) || ($result == 'error=11') || ($result == 'err:1'))
return 'error';
$obj = json_decode($result);
return isset($obj->livearray->livesummary->cur_online) ? $obj->livearray->livesummary->cur_online : 0;
}
}
$html = file_get_contents('http://histats.com/viewstats/?sid=3041076&act=2&f=1');
preg_match("/OBJ_summary.sockTOKEN = '(.*?)'/i", $html, $match);
echo 'Online: '. getHistats('3041076', 'bjh1NStBTVZyMFJzRENTODFHTHNQamJyV0FvY2l4TGRNSk5FczQyYnR3dERlaUhWczJZNUtWQk5lU2p6STlyRTZCQXZUd2t6MWJzS3Z2cWs2d1g4aXc9PQ==');
echo '<br />';
echo 'Token: '. $match[1];
echo '<br />';
echo 'Online: '. getHistats('3041076', $match[1]);

Related

I have made a proxy scraper in PHP but I don't know how to check if the proxy is live

The below code scrapes the proxy from the website but what I want is the program to check if the proxy is alive or not one by one and then save that proxy in the file. Can someone help me out to do so
<?php
header('Content-Type:application/json');
$url = "https://www.my-proxy.com/free-proxy-list.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/'.rand(111,999).'.36 (KHTML, like Gecko) Chrome/88.0.'.rand(1111,9999).'.104 Safari/'.rand(111,999).'.36');
curl_setopt($ch, CURLOPT_URL, $url);
$proxies = array();
$firstcount = 1;
$endcound = 10;
for ($i = $firstcount; $i <= $endcound; $i++){
curl_setopt($ch, CURLOPT_URL, "https://www.my-proxy.com/free-proxy-list-$i.html");
$result =curl_exec($ch);
///Get Proxy
// >102.64.122.214:8085#U
preg_match_all("!\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}:.\d{2,4}!", $result, $matches);
$proxies = array_merge($proxies, $matches[0]);
}
curl_close($ch);
print_r($proxies);
?>
There are multiple ways to test, easiest one being an option in 'file_get_contents' request
$options = array(
'http'=>array(
'proxy' => 'tcp://' . $prox, //IP:PORT info. ie: 8.8.8.8:2222
'timeout' => 2,
'request_fulluri' => true,
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36\r\n"
)
);
$context = stream_context_create($options);
$base_url='http://lotsofrandomstuff.com/1.php'; //url that simply returns '1' each time
$web=#file_get_contents($base_url,false,$context);
if($web=='1')
{
echo "proxy is good";
}
else
{
echo "proxy is dead";
}

Can't Return When Looping

Why I can't return while looping in function? Why I just got 1 result like without looping? Here is my code:
function search($get){
$i=0;
//print_r($get);
foreach($get->itemlist as $song){
$i++;
$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'wmid=14997771; user_type=2; country=id; session_key=96870dd03ab9280c905566cad439c904;');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36');
$json = curl_exec($ch);
$json = str_replace('MusicInfoCallback(', '', $json);
$json = str_replace(')', '', $json);
$json = json_decode($json);
$songurl = $json->mp3Url;
//print_r($json);
return array($i => array("song" => $json->msong,
"singer" => $json->msinger,
"url" => $song->songid));
}
}
print_r(search("key"));
any alternative?
Untested Code:
function search($get){
foreach($get->itemlist as $song){
$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, 'wmid=14997771; user_type=2; country=id; session_key=96870dd03ab9280c905566cad439c904;');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36');
$json = curl_exec($ch);
$json = json_decode(substr($json,18,-1),true);
$results[]=['songurl'=>$json['mp3Url'],
'song'=>$json['msong'],
'singer'=>$json['msinger'],
'url'=>$song->songid
];
}
return $results;
}
I don't have any sample data to verify my code with. I am making an assumption that 'MusicInfoCallback( and ) are the start and end of the curl string. I recommend packing all data into an (automatically) indexed array.
$songurl was also "trapped" within the scope of the function.

PHP Curl Tor Pages Not Displaying

I have this code
<?php
$ua = array(
"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)",
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; XH; rv:8.578.498) fr, Gecko/20121021 Camino/8.723+ (Firefox compatible)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1",
"Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (X11; U; Linux i686; fr-fr) AppleWebKit/525.1+ (KHTML, like Gecko, Safari/525.1+) midori/1.19",
"Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
"Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
$uar = array_rand($ua);
$url = "sometestserverisetup";
$ip = '127.0.0.1';
$port = '9051';
$auth = 'mypwwhateveritis';
$command = 'signal NEWNYM';
$fp = fsockopen($ip,$port,$error_number,$err_string,10);
if(!$fp) { echo "ERROR: $error_number : $err_string";
return false;
} else {
fwrite($fp,"AUTHENTICATE \"".$auth."\"\n");
$received = fread($fp,512);
fwrite($fp,$command."\n");
$received = fread($fp,512);
}
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:9050");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch,CURLOPT_USERAGENT,$ua[$uar]);
$response = curl_exec($ch);
echo $response;
?>
everything works fine. With my test site and it displays correctly. However certain sites (google.com, amazon.com, youtube, facebook. only display a blank page for echo response.
Is there some curl set opt that needs to be enabled for pages to display properly.
Looking at a var_dump(curl_getinfo($ch)); after calling curl_exec can be helpful.
I tested your code and found in some cases the sites send a 302 Moved response with a Location header to redirect the browser which would result in an empty response on a successful request.
Adding
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
made it so that every site you mentioned always returned a response in my tests. And depending on what you are doing (searches, logins, form submissions) you will probably find redirects are common so you need to tell cURL to follow them with that option.
Beyond that, you can set CURLOPT_HEADER to true so you can look at the response headers sent to see what's going on in addition to curl_getinfo to make sure the connection was successful (either through Tor or to the site).

Make request with custom headers (POST)

My code isn't working, tried a few things but I'm new to php so yeah... here's what I got, always returns me a blank page.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$rnd = $_GET['rnd'];
$ch = curl_init("http://chat.website.com/script/login.php?rnd=".$rnd);
$request_headers = array();
$request_header[] = (
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded',
'onLoad: [type Function]',
'p: password',
'u: username',
'owner: [object Object]
');
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$userdata = curl_exec($ch);
echo $userdata;
?>
you are passing $request_headers but the data you have in $request_header and better see your array is fine.
or may be try something like this:
$request_header[] = array('User-Agent'=>'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36',
'Content-Type'=> 'application/x-www-form-urlencoded',
'onLoad'=>'[type Function]',
'p'=>'username',
'u'=>'password',
'owner'=>'[object Object]
');
I found my error, I wasn't making the request in POST.
Here's the code that is working if anyone needs it:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$rnd = 1;
$rnd = $_GET['rnd'];
$ch = curl_init("http://chat.website.com/scripts/login.php?rnd=".$rnd);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "onLoad=%5Btype%20Function%5D&p=password&u=username&owner=%5Bobject%20Object%5D");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$userdata = curl_exec($ch);
echo $userdata;
?>

Testing a static variable in a function with PHPUnit

I have the following simplified code to test, which is an included function in our library that controls rendering for many different devices.
<?php
define('CHROME_18_0_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.152 Safari/535.19');
define('FIREFOX_13_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0');
define('FIREFOX_16_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0');
define('IE_8_HTTP_USER_AGENT',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)');
define('IE_9_HTTP_USER_AGENT',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');
define('OPERA_11_5_HTTP_USER_AGENT',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.9.168 Version/11.50');
define('SAFARI_5_0_HTTP_USER_AGENT',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27');
function IsIe()
{
static $IsInternetExplorer;
if (!isset($IsInternetExplorer))
{
$Server = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : 'UNKNOWN';
$IsInternetExplorer = ((strpos($Server, 'MSIE') === FALSE) ? FALSE : TRUE);
}
return $IsInternetExplorer;
}
/**
* #backupGlobals enabled
*/
class TEST_IsIEFunctions extends PHPUnit_Framework_TestCase
{
protected $backupGlobals = TRUE;
public function UserAgentDataProvider()
{
return array(
'Chrome 18' => array(CHROME_18_0_HTTP_USER_AGENT, FALSE),
'FireFox 13' => array(FIREFOX_13_HTTP_USER_AGENT, FALSE),
'Opera 11' => array(OPERA_11_5_HTTP_USER_AGENT, FALSE),
'Safari 5' => array(SAFARI_5_0_HTTP_USER_AGENT, FALSE),
'IE 8' => array(IE_8_HTTP_USER_AGENT, TRUE),
'IE 9' => array(IE_9_HTTP_USER_AGENT, TRUE),
);
}
/**
* #backupGlobals TRUE
* #dataProvider UserAgentDataProvider
*/
public function testIsIeFunction($UserAgent, $IsIeResult)
{
$_SERVER['HTTP_USER_AGENT'] = $UserAgent;
$this->assertEquals($IsIeResult, IsIe());
}
}
?>
Error Output:
Q:\>phpunit TestIe.test
PHPUnit 3.7.13 by Sebastian Bergmann.
....FF
Time: 0 seconds, Memory: 2.50Mb
There were 2 failures:
1) TEST_IsIEFunctions::testIsIeFunction with data set "IE 8" ('Mozilla/4.0 (comp
atible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727;
.NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322;
.NET4.0C; .NET4.0E)', true)
Failed asserting that false matches expected true.
Q:\TestIe.test:60
2) TEST_IsIEFunctions::testIsIeFunction with data set "IE 9" ('Mozilla/5.0 (comp
atible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)', true)
Failed asserting that false matches expected true.
Q:\TestIe.test:60
FAILURES!
Tests: 6, Assertions: 6, Failures: 2.
I am running PHPUni 3.7.13 with PHP 5.4.11, 5.3.22.
I have tried setting the various versions of the backupGlobals (comment section and protected variable) to TRUE and FALSE without success. Any suggestions would be appreciated.
Update:
Also tried the backupStaticAttributes with the commented #backup and protected $backup

Categories