Ok so the instructions to connect to this API are:
curl -X GET "https://api.xxxxxxxxxxx.com/jobs?limit=10&offset=0" -H "accept: application/json" -H "X-SmartToken: xxxxxxxxxxxxx"
So my PHP looks like the below but I am getting this [message] => Authentication data missing
$token = "xxxxxxxxxxxxx";
$url = "https://api.xxxxxxx.com/jobs?limit=10&offset=0";
header('Content-Type: application/json');
$ch = curl_init($url);
$authorization = "Authorization: X-SmartToken ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
print_r(json_decode($result));
exit;
Anyone see what I am doing wrong?
Thanks
$authorization = "X-SmartToken: ".$token;
The : was missing and the Authorization needed to be removed.
You should not prepend Authorization: to the HTTP header:
$authorization = "X-SmartToken: " . $token;
Related
I'm trying to make a request using curl with php, but I'm getting the following error. Could I be using curl wrong?
I generate $jwt on top lines
exec("curl -v -H 'Authorization: Bearer $jwt' \"https://api.storekit.itunes.apple.com/inApps/v1/refund/lookup/1234\"")
Could not resolve host: Bearer
This is how I can use it on this site apple
You can use curl like this
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.storekit.itunes.apple.com/inApps/v1/subscriptions/{original_transaction_id}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer '. $jwt;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
var_dump($result);
curl_close($ch);
I want to make a simple call to an API from PHP. It's working fine directly in CURL but not in PHP 7.3.
My real token was replace by TOKEN
CURL:
curl -X POST -H 'Authorization: Bearer TOKEN' -H "Content-Type: application/json" --data-binary '{"query":"{cameraList{name}}"}' https://cloud.camstreamer.com/api/graphql.php
PHP:
<?php
$url = "https://cloud.camstreamer.com/api/graphql.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer TOKEN";
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"query": "{cameraList{name}}"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
exit;
You can use
$info = curl_getinfo($ch);
$error = curl_error($ch);
to see what happened.
The content type was not sent correctly.
I changed this:
$headers = array();
$headers["Authorization"] = 'Bearer TOKEN';
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
To this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer TOKEN'
));
For getting connected to an API endpoint, it is necessary to include in the request a token, which should be received before.
Example:
curl -X GET \
https://api.mysite/v2/orders \
-H 'Authorization: Bearer <token>'
In PHP this will look like this:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v2/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: Bearer <token>';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
...
Authorization (OAuth2)
Fetch access token
curl -X POST \
https://api.mysite/v1/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'cache-control: no-cache' \
-d 'username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-mysite-api'
PHP version:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v1/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=<YourUsername>&password=<YourPassword>&grant_type=password&client_id=token-mysite-api");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
$access_token = $result['access_token'];
$refresh_token = $result['refresh_token'];
The response looks like this:
{
"access_token": "<access tocken>",
"expires_in": 1800,
"refresh_expires_in": 7200,
"refresh_token": "<refresh tocken>",
"token_type": "bearer",
"not-before-policy": 0,
"session_state": "916a880a-9dae...",
"scope": "pumba-roles-app-portal partner email profile"
}
Once the access token is expired should be used the refresh token to get a new access token, without using users credentials again:
curl -X POST \
https://api.mysite/v1/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'cache-control: no-cache' \
-d 'refresh_token=<refresh_token>&grant_type=refresh_token&client_id=token-mysite-api'
PHP version:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.mysite/v1/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "refresh_token=<refresh_token>&grant_type=refresh_token&client_id=token-mysite-api");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Cache-Control: no-cache';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
...
The response from refresh is the same as from getting token (see it above).
My question is:
How to do the verification and get the correct token? If the access_token is not expired > use it again. If it is expired > use the refresh_token for getting the new access_token and use it. Repeat this action for the next requests.
I spent much time trying different approaches but with each request, I get always new token.
Waiting for your help, please :).
Guys. I am new to CURL so i have no experience on implementing CURL request. In this case, i want to post some data using CURL. Here is the CURL :
curl -H 'Authorization: Bearer <ACCESS_TOKEN>' \
-H 'Content-Type: application/json' \
-d '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}' \
https://partner.path.com/1/moment/photo
is there any of you guys know about implementing CURL request using the above data? Thank you very much.
finally, i found the answer. Here is how i create a request to the Path API
$url = 'https://partner.path.com/1/moment/photo';
$authorization = "Authorization: Bearer 8edf232243d58e4940d931490e882123432434f";
$headers = array($authorization,'Content-Type: application/json');
$json_data = '{"source_url": "http://url/to/photo.jpg", "caption": "I like cheese!"}';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );
How can i send a CURL request mentioned below in PHP? What functions i will use in php?
$ curl -H 'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0' \
-H 'Accept: application/json' \
'http://example.sifterapp.com/api/projects'
I have tried this code.. but its not working..
Please do the needful
$curlString = "";
$curlString .= "-H \"X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0\" \";
$curlString .= "-H \"Accept: application/json\" \";
$url="http://example.sifterapp.com/api/projects";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlString);
$data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
// Show me the result
var_dump($data);
curl_close($ch);
}
You do not use correctly CURLOPT_HTTPHEADER. From the manual:
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the
format array('Content-type: text/plain', 'Content-length: 100')
So you would need:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Sifter-Token: 343b1b831066a40e308e0af92e0f06f0',
'Accept: application/json',
));