Issue in adcash api curl code - php

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.

Related

API call with Curl

I need to call this command using curl.
http -a $CLIENT_ID:$CLIENT_SECRET --form POST https://api-sandbox.com/auth/token grant_type=client_credentials scope=access_token_only
I have tried following, but not getting through
$URL = "https://api-sandbox.com/auth/token";
$data = array(
'grant_type' => "client_credentials",
'scope' => "access_token_only"
);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_APPEND, "$CLIENT_ID:$CLIENT_SECRET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$data=curl_exec($ch);
curl_close($ch);
I am getting following error
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
How can I do it?
Thx
----------------Additional Information -----------------
oAuth2.0 is being used for authentication
This code should work for you
<?php
$URL = "https://api-sandbox.com/auth/token";
$data = [
'grant_type' => "client_credentials",
'scope' => "access_token_only",
];
$client_id = getenv('CLIENT_ID');
$client_secret = getenv('CLIENT_SECRET');
try {
$ch = curl_init();
// Check if initialization had gone wrong*
if ( $ch === false ) {
throw new RuntimeException( 'failed to initialize' );
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-sandbox.com/auth/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=access_token_only");
curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . $client_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$content = curl_exec( $ch );
curl_close( $ch );
if ( $content === false ) {
throw new RuntimeException( curl_error( $ch ), curl_errno( $ch ) );
}
/* Process $content here */
// Close curl handle
curl_close( $ch );
} catch ( RuntimeException $e ) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
It worked in a following way:
$content = "grant_type=client_credentials&scope=access_token_only&client_id=".$client_id."&client_secret=".$client_secret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-sandbox.com/auth/token');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

How to post image using curl in php

I want to change profile image another application curl request i am trying below code can anyone help me please
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
As Anoxy said, you need to put in the header the Content-Type :
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: multipart/form-data');
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}

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();

php curl scrape not working

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));
}

Can't use file_get_contents on https://api.facebook.com

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.

Categories