Get access from an API in PHP - php

im new in PHP, and I have a task to get a data from my client using an API..
but the thing is, can't understand this API even after researching and trying about this..
My client has given me details for the API
URL - http://222.126.31.19:8084/api/login
Username - admin123
Password - test123
those are just a dummy values..
my client also said value should be the token response from login api and prepended by the word "Bearer "
After doing a research.. I tried this codes but nothing appears on my browser.. any idea or suggestion that I can do..
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "admin123:test123");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
Do i need to put this on a button or something? Sorry If I'm asking here, It's been a day and I can't solve it.

Most REST API's are working with some kind of session-id. If you are logging in, you will get an session-id for further calls and a list of possible calls for your login-credentials. But every API is working different. Like BT643 said, they have to give you some form of documentation. I use the following code to access some API's i am using. Try it out, if you like.
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://222.126.31.19:8084/api/login');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'admin123' . ":" . 'test123');
$out = json_decode(curl_exec($ch));
curl_close($ch);
Then you can print out the return-result (if there is any) with:
echo '<pre>'
var_dump($out);
echo '</pre>';
This is just to look if you get any result from your call.

Related

cURL post api call with authentification on Aliseeks api

I am trying to connect to the Aliseeks api with CURL but I am not sure where to put my api key.
On the API's doc they say
"Aliseeks expects for the API key to be included in all API requests
in a header that look like the following: X-Api-Client-Id: Your Api
Key"
Now I am doing this :
$url = 'https://api.aliseeks.com/v1/products/details';
$curl = curl_init();
$auth_data = array(
"X-Api-Client-Id" => 'my_api_key_here',
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, $auth_data );
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
But in the return I get :
[{"code":"no_token_found","violation":"aliseeks.authentication.notokenfound"}]
Any idea ?
ps: the $auth_data is here twice because I am trying kind of everything now...
You are using a wrong constant to send the headers.
It should be CURLOPT_HTTPHEADER instead of CURLOPT_HEADER.

PHP CURL in iframe change base url?

I'm using a CURL call to an API. The call gives back a HTML DOM tree that i echo inside an iframe. The problem is that the base url refers to my own site instead of the API's site. For example: <script type="text/javascript" src="/swf/swfobject.js"></script> will refer to my website instead of the API's. I have looked at the CURL set options but i couldn't find what i'm looking for, could someone please help me? I tried using preg_replace which worked but i have a lot of diffrent inpredictable links. (and its way to dirty!)
Code of CURL call:
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($curl);
}
$output = CallAPI($method, $url, $data);
echo $output;
HTML:
<iframe src="videorecorder.php" frameBorder="0" scrolling="no"></iframe>
Please help me!
Thanks
CURLOPT_REFERER
curl_setopt($curl, CURLOPT_REFERER, $referer);

Script for secure cPanel login with PHP

Since recently, cPanel has changed the way that it logs in.
Before login, the url is : https://accessurl:2083/
After login : https://accessurl:2083/cpsessXXXX/frontend/x3/index.html?post_login=89711792346495
You will note the cpsessXXXX embedded in the url.
And the page to access AWSTATS is :https://accessurl:2083/cpsessXXXX/awstats.pl?config=domain_name&ssl=&lang=en
I have tried the following PHP code
$username = 'xxx';
$password = 'xxx';
$loginUrl = 'https://<accessurl>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT,2083);
$store = curl_exec($ch);
curl_close($ch);
When I step through the code, the value of $store is FALSE, meaning that the login process failed.
The only reference that I found on the web to a similar problem is
at http://blog.mcfang.com/ in the March 28 entry.
I was hoping that cookies.txt would have the cpsessXXXX info, but no file is created.
Thanks for any help
You need to reference the security token back to cPanel in order for your script to be accepted. As per the documentation over at cPanel documentation for Security tokens
Take the following example:
function createSession() { // Example details
$ip = "127.0.0.1";
$cp_user = "username";
$cp_pwd = "password";
$url = "http://$ip:2082/login";
$cookies = "/path/to/storage/for/cookies.txt";
// Create new curl handle
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Save cookies to
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=$cp_user&pass=$cp_pwd");
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
// Execute the curl handle and fetch info then close streams.
$f = curl_exec($ch);
$h = curl_getinfo($ch);
curl_close($ch);
// If we had no issues then try to fetch the cpsess
if ($f == true and strpos($h['url'],"cpsess"))
{
// Get the cpsess part of the url
$pattern="/.*?(\/cpsess.*?)\/.*?/is";
$preg_res=preg_match($pattern,$h['url'],$cpsess);
}
// If we have a session then return it otherwise return empty string
return (isset($cpsess[1])) ? $cpsess[1] : "";
}
cpsess is used to append the URLs the correct token that cPanel expects back.
You can send the "Authorization" row by Headers adding this simple row:
$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
So you can send a request directly to the resource $myResource, without cookies and anything else.
Simply:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $myResource);
$header[0] = "Authorization: Basic " . base64_encode($cp_user.":".$cp_pwd) . "\n\r";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT,2083);
$store = curl_exec($ch);
curl_close($ch);
You should consider to use the XML API of CPanel. On Github you can find the xmlapi-php class, it works great and help me to keep the code simple and easy to update!

How to get JSON response using curl

Hey there I am started to learn foursquare API, but I am stuck at getting an Access Token.
Here is a part from the code I found in SO.
// build url
$url = 'https://foursquare.com/oauth2/access_token';
$url .= '?client_id='.CLIENT_ID;
$url .= '&client_secret='.CLIENT_SECRET;
$url .= '&grant_type=authorization_code';
$url .= '&redirect_uri=**********/callback'; //change to your 4sq callback
$url .= '&code='.$code;
// call to https://foursquare.com/oauth2/access_token with $code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
However this did not work, so I have tried to find error. First echoed $url and manually clicked on that link. It worked, foursquare has returned me an access token in json format. So the problem is in the curl part of the code.
Can you find my error? and more importantly can you suggest me some resources to study on curl?
EDIT:
I did a var_dump($result) and the output is 'boolean false'
The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
If it is a POST a request then this is the proper way to do it :
$body.='client_id='.CLIENT_ID etc.
$c = curl_init ();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);

google access api with php curl

I am trying to fetch my user's emails with google api & curl, and I am stuck on redeeming access token. I get the key no problem, but when it comes to token, google just returns json
{"error" : "invalid_request"}
below is a snippet which redeems code for token (well at least it supposed to)
$code = urlencode($_GET["code"]);
$secret = "MYSECRET";
$client_id = "MYCLIENTID";
$redirect_uri = urlencode("http://www.mysupersite.com/callback.html");
$grant_type = "authorization_code";
$url = "https://accounts.google.com/o/oauth2/token";
$headers = array("Content-type: application/x-www-form-urlencoded");
$post = "code=".$code."&client_id=".$client_id."&secret=".$secret."&redirect_uri=".$redirect_uri."&grant_type=".$grant_type;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$json = curl_exec($curl);
curl_close($curl);
var_dump($json);
I'm really stuck and google api never tells you what exactly is wrong.
UPD I know there's a lib for this kind of calls, but I'm solving one simple task (well according to google api documentation) and simply see no point in including loads of other methods.
okay my bad - $_GET["secret"] needs to be $_GET["client_secret"]

Categories