google access api with php curl - php

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"]

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.

PayPal payouts sandbox is giving AUTHORIZATION_ERROR

I am using PayPal payouts sandbox API. In my sandbox app, payouts are enabled. But when I hit the API endpoint then it gives AUTHORIZATION_ERROR although my client_id & client_secret are correct. Following is the code sample:
function get_paypal_auth_token($client_id, $client_secret) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, $client_id.":".$client_secret);
curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result);
}
function create_paypal_payment($data) {
$client_id = "XXXXXX";
$client_secret = "XXXXXXXSECRET";
$auth_data = get_paypal_auth_token($client_id, $client_secret);
$url = "https://api.sandbox.paypal.com/v1/payments/payouts";
$data_json = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$auth_data->access_token));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
return json_decode($resp);
}
The expected result of this API should be kind of related to payouts.
Also, I try it with another account's credentials then it gives INSUFFICIENT_FUNDS error.
So can anyone please tell how to add balance to my PayPal account?
Do you get this error only sometimes? I think their sandbox is experiencing issues, as my system also gets this error about 50% of the time since a couple of days ago. You can try sending a direct request to their servers to confirm this.
Follow these steps to add money in your sandbox account
Login to your Paypal sandbox using your sandbox account.
Click My Account –> Add Money
Click the type of account
Enter the amount

Get access from an API in 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.

Simple php code in post method which request from web service

I am new to web services. I have tried to request from the example web service at http://www.w3schools.com/webservices/tempconvert.asmx. This is the code I have tried:
<?php
$data = array('Celsius' => '56');
$curl = curl_init('http://www.w3schools.com/webservices/tempconvert.asmx
/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, 'http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
I am not entirely sure, but should the data be a query string
$data = "Celsius=56";
I am sure you are doing this just to test curl. You don't really need a webservice to convert celsius to farenheit
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($curl, CURLOPT_POST,count($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
Use these attibutes. to fetch the data. as some url requires exact parameters.
Note* :- Headers are optional

how can I create a function that can post my parameters using curl

I developed a API for my website. API working 100% correctly. But I designed it to catch and execute functions from this kind of POST parameters$_POST['functionName']; $_POST['parameter']; Now, in request page i want to send those as $_POST, I managed to do deal with that. But my problem is, how can I create a function that can post my parameters for each functions in API using curl. Here is my code which send requests to my API.
$url = 'http://localhost/myapi/api.php';
$postfields['functionName'] = "myFunction";
$postfields['parameter'] = "1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$data = curl_exec($ch);
$results = json_decode($data, true);
curl_close($ch);

Categories