How to change socks5 if dead cURL - php

i have script to check something with socks all is ready but when its socks5 dead not change to try other socks from list, look below and please help anyone to fix that :
$ch = curl_init();
if($sock!=''){
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_PROXY, $sock);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dattt);
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(

Try something like this
<?php
$hosts = array(
'xx.com',
'yy.com'
);
class TimeoutException extends \Exception {
}
class Curl
{
public function connect($sock, $ch, $dattt, $url)
{
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
curl_setopt($ch, CURLOPT_PROXY, $sock);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dattt);
// etc
$result = curl_exec($ch);
// 28 is curl timeout
if (curl_errno($ch) === 28) {
throw new TimeoutException;
}
// some other error handling...
return $result;
}
}
$curl = new Curl;
foreach ($hosts as $socks) {
try {
$result = $curl->connect($socks, $curlResouece, $dattt, $url);
break;
} catch (TimeoutException $e) {
continue;
}
}
if (!isset($result)) {
die('Failed all socks');
}
echo $result;
As for an explanation, you're looping through the socks proxies you've specified. If the CURL error is 28, it'll throw a timeout exception which you catch in the try catch block. You can then continue to the next proxy. If is has a result, it'll break from the foreach and return the result straight away. If all of the socks proxies are unavailable, it'll die an error.

Related

Check proxy before cURL

I need to check whether the selected $proxy is active or not and do a loop until a working one in proxy.txt is found before using it in a cURL. How could I do that? Below is a given proxy function.
<?php
function proxies()
{
$proxylist = file("proxy.txt");
$randomproxy = rand(0, sizeof($proxylist) - 1);
$proxylist = $proxylist[$randomproxy];
$words = explode(':', $proxylist);
return $words;
}
$proxy = proxies();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
....
?>
Thank you.
maybe you can ping, something like this
function proxies()
{
return file("proxy.txt");
}
foreach ($proxies() as $proxy) {
$item = explode(':', $proxy);
$waitTimeoutInSeconds = 1;
if ($fp = fsockopen($item[0], $item[1], $errCode, $errStr, $waitTimeoutInSeconds)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
break;
}
}
fclose($fp);

cURL request freeze the browser

I try to request an URL width cURL, but there is no answer and the option CURLOPT_CONNECTTIMEOUT doesn't work.
I have no answer at all... I suppose that the server doesn't answer. I try to use the Chrome plugin called Restlet Client - REST API Testing to test the URL, but no answer too.
private function curlInterrogation(){
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
$result = curl_exec($ch);
curl_close($ch);
return $result;
} catch (Exception $ex) {
Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
return array();
}
}
Each time i run my function, i freeze my browser for the domain (i can browse in other website) and i have to restart my browser.
With the CURLOPT_CONNECTTIMEOUT option, should the execution stop and return that it can not connect to server ?
Thanks for your help
Try to check cURL Php Manual if you're trying to check cURL info.
Nonetheless, just check Php cURL Manual.
Then going to your code (Check my comments):
private function curlInterrogation(){
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
$result = curl_exec($ch);
curl_close($ch); //You've just closed the execution early before return
return $result; //Returning just the execution is most likely an empty page
} catch (Exception $ex) {
Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
return array();
}
}
Try this
function TestCurl(){ //Just function
try{
$base_url = 'https://www.example.com'; //Replace this with your client URL
$user_name = 'user'; //Replace with username
$user_pass = 'pass'; //Replace with password
$ch = curl_init(); //Initializes a new session and return a cURL handle
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $user_name.":".$user_pass);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLINFO_HEADER_OUT, false );
curl_exec($ch);
$info = curl_getinfo($ch);
echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
curl_close($ch); // <- Always put this at the end of the execution
}
catch (Exception $e) {
echo $e->getMessage();
}
}
//Call TestCurl function
TestCurl();

How check private proxy with CURL?

I need check proxy with CURL in my script, but there is error "couldn't connect to host".
Please see the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'xx.xxx.xx.103:8080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'login:pass');
$data = curl_exec($ch);
if ($data === false)
{
echo "Proxy is not working: ", curl_error($ch);
}
else
{
echo "OK";
}
Thanks to all who can help!
Remove port after ip and set port in given cURL option below
curl_setopt($ch, CURLOPT_PROXY, 'xx.xxx.xx.103');
curl_setopt($ch, CURLOPT_PROXYPORT, '8080');

Curl not able to get response in same server

I am using curl in my application.
My function in DefaultController:
public function actionWebServices()
{
$data = array("account" => "1234", "dob" => "30051987", "site" => "mytestsite.com");
$fields='';
foreach ($data as $key => $value)
{
$fields .= $key . '/' . $value . '/';
}
rtrim($fields, '&');
$u='admin';
$p='admin123';
$url='localhost:83/Working-copy/mysite/ServiceTypeMaster/Test';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, $u.':'.$p);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
session_write_close();
$result = curl_exec($ch);
curl_close($ch);
session_start();
return $result;
}
In serviceTypeMasterController:
public function actionTest()
{
echo "test";
}
I am calling URL of localhost in curl URL. but curl doesn't give any response. It only gives me only Response Doesn't contain any data.
What is wrong?
Typically, that response is if the page/service does not exist. Are you sure you're running your server on port 83 (and not on default 80) and that the url resolves to your actionTest (instead of test) method?

PHP function to connect to pingomatic using cURL

I'm creating a PHP function to connect to pingomatic using CURL but the response is always.
Array ( [EXE] => XML-RPC server accepts POST requests only. )
here is my sample code...
function curl_getpage2($url,$data, $referer = null, $agent = null, $header = null, $timeout = 20, $proxy = null, $proxy_username = null, $proxy_password = null) {
//getProxy();
if ($agent == null) {
$agent = getAgent();
}
if ($referer == null) {
$referer = getHost($url);
}
if (!is_array($header)) {
$header = array("Content-Type:text/xml","Host:".getHost($url),"User-Agent:$agent",
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language:en-us,en;q=0.5",
"Accept-Encoding:gzip,deflate",
"Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive:300",
"Connection:keep-alive",
"Cache-Control:max-age=0",
"Content-length: ".strlen($XML));
}
if($proxy == null){
$proxy = "208.100.27.155:60099";
}
if($proxy_username == null && $proxy_password == null){
$proxyUnPW = "unproxy:pwproxy";
}else{
$proxyUnPW = $proxy_username.":".$proxy_password;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,$proxyUnPW);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
//curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//curl_setopt($ch, CURLOPT_REFERER, $referer);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//curl_setopt($ch, CURLOPT_AUTOREFERER,TRUE);
//curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
//curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result['EXE'] = curl_exec($ch);
//$result['INF'] = curl_getinfo($ch);
//$result['ERR'] = curl_error($ch);
curl_close($ch);
return $result;
}
I need some help here. I'm just somewhat new to PHP.
You can submit your site once via the web form and then call a GET request for that url (see
question How to ping automatically to pingomatic in PHP?).
So your URL would be something like the following if you want to ping all services (title and url encoded with urlencode()):
$url = 'http://pingomatic.com/ping/?title='.$title.'&blogurl='.$url.'&rssurl=&chk_weblogscom=on&chk_blogs=on&chk_feedburner=on&chk_newsgator=on&chk_myyahoo=on&chk_pubsubcom=on&chk_blogdigger=on&chk_weblogalot=on&chk_newsisfree=on&chk_topicexchange=on&chk_google=on&chk_tailrank=on&chk_skygrid=on&chk_collecta=on&chk_superfeedr=on');
eg:
http://pingomatic.com/ping/?title=Example%20Title&blogurl=http%3A%2F%2Fwww.example.com%2F&rssurl=&chk_weblogscom=on&chk_blogs=on&chk_feedburner=on&chk_newsgator=on&chk_myyahoo=on&chk_pubsubcom=on&chk_blogdigger=on&chk_weblogalot=on&chk_newsisfree=on&chk_topicexchange=on&chk_google=on&chk_tailrank=on&chk_skygrid=on&chk_collecta=on&chk_superfeedr=on

Categories