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();
Related
I have a curl code with me shown below :
curl --request GET \
--user 60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR:API-P4ss \
https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2
I wanted to write this code in a php file. I dnt know how to start writing this code. Can anyone tell how to write curl code in php ?
Try this:
$url = 'API URL';
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$APIresponse = curl_exec($ch);
curl_close($ch);
$res = json_decode($APIresponse, true);
// $res contains the API response as an array
}
catch(Exception $e)
{
// Error mesage
}
Reference
You need to use PHP Client URL Library.
Solution for your code:
<?php
$username = "60CF3Ce97nRS1Z1Wp5m9kMmzHHEh8Rkuj31QCtVxjPWGYA9FymyqsK0Enm1P6mHJf0THbR";
$password = "API-P4ss";
$process = curl_init("https://api.sandbox.ewaypayments.com/Transaction/44DD7aVwPYUPemGRf7pcWxyX2FJS-0Wk7xr9iE7Vatk_5vJimEbHveGSqX52B00QsBXqbLh9mGZxMHcjThQ_ITsCZ3JxKOY88WOVsFTLPrGtHRkK0E9ZDVh_Wz326QZlNlwx2");
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
var_dump($return);
The response of API is in $return variable.
First use file_get_contents php function.
If your not found solution then try below code
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
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.
My scraping code is not working , I used php cURL , But not returning page content . So please help me , how can I get all content .
function getSslPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$url = "https://www.woolworths.com.au/Shop/SearchProducts?search=Fresh%20Apple";
$ch = getSslPage($url);
echo $ch;
Add this right after your curl_exec() call and copy/paste here the output:
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
All,
I have written a curl to get the details from Adcash API. Output of this API is to get the token number after login.
Below code is working good but it is not getting the token as output. It is null. Any suggestions.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info) ;
echo $output;
if (FALSE === $output)
throw new Exception(curl_error($ch), curl_errno($ch));
Echo "Executing Completed";
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I have updated the code to get the subid report. please check and let me know what is the issue.
<?php
try{
Echo "Executing started";
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata = array (
'login' => 'xxx',
'password' => 'xxx'
);
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$json_a=json_decode($output,true);
$token= $json_a["token"];
curl_close($ch);
$url = "https://www.adcash.com/console/login_proxy.php";
$ch = curl_init();
$logindata1 = http_build_query($logindata);
curl_setopt($ch, CURLOPT_POSTFIELDS, $logindata1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "token=". $token . "&call=get_publisher_detailed_statistics");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($output);
curl_close($ch);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
?>
I'm part of Adcash IT team. First of all, thanks for using our API. The problem in your code is that you're using HTTP authentication. Our API is using POST.
This part of your code:
curl_setopt($ch, CURLOPT_USERPWD, "userid:password");
Can be replaced by:
$logindata = array (
'login' => YOUR_LOGIN_HERE,
'password' => YOUR_PASSWORD_HERE
);
$logindata = http_build_query($logindata);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_URL, 'https://www.adcash.com/console/login_proxy.php');
Let me know if it works.
I am trying to get some information using https://api.facebook.com
The following works just fine: (notice graph.facebook.com)
$q = "https://graph.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
But
$q = "https://api.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
Just does not work. I've tried curl, but I keep getting Method Not Implemented
Following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = "https://api.facebook.com/$params".$sep."access_token=$access_token";
echo $url;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
echo curl_exec($ch);
echo curl_errno($ch);
curl_close ($ch);
Any ideas?
Such a silly mistake. I post the working example:
The call:
$status = $fb->api("method/fql.query?query=".urlencode("SELECT url FROM url_like WHERE user_id = $ID")."",$access_token);
The class:
class fb
{
public function api($params, $access_token)
{
try
{
$q = "https://api.facebook.com/$params?format=json-strings&access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
if ($response->error->message != null)
throw new Exception($response->error->message);
else
return $response;
}
catch (Exception $e)
{
return ("<b>Error:</b> " . $e->getMessage());
}
}
}
For everyone who don't want to use the facebook php sdk.