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.
Related
Hi I'm trying to consume an API.
I need to add the API key in the header
API endpoint = http://overheid.io/api/kvk
if you want to test things, you can create an account here: https://overheid.io/auth/register
Documentation is in Dutch and can be found here: https://overheid.io/documentatie/kvk
This is what I came up with but do not get passed authentication.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER => true);
curl_setopt($curl, CURLOPT_HTTPHEADER, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
?>
Edit: I've replaced the initial code part, with the solutions, but still no success.
Can anyone point me in the right direction?
Thanks!
You can try to use the CURLOPT_USERPWD option to pass the Authentication header. You also seemed to have some syntax errors.
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERPWD, "ovio-api-key:the_api_key");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
But, this API seem to use a custom header...so you can try the following if it the above does not work:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: yourkey"));
UPDATE:
I used the following with an account I created on their site:
<?php
$service_url = 'https://overheid.io/api/kvk';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("ovio-api-key: key"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
curl_close($curl);
print $curl_response;
This worked for me when I replaced the "key" part with the 64 character key they provide.
I have this code for logging into Google using Simple DOM Parser with curl. I've tried to post status on google plus using following code but unable to post on google plus
Any idea on how to solve this?
Here's my code for reference:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youremail#gmail.com",
"Passwd" => "yourpassword",
"service" => "writely",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$params['newcontent'] = "Post on Google Plus Test By Me";
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl);
curl_close($curl);
Thanks For your help and co-operation
There is not a Google+ API to post statuses (unless you are a Google Apps user) and ClientLogin has been deprecated and will stop working soon. You're best bet is to look into using something like the share plugin.
In order not show the moved you have to set the agent header like firefox or something else
example
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);`enter code here`
You need to add these two cURL params here too.
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
Also, you are not outputting any response on your code
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);
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
I'm attempting to use the Github v3 API and posting JSON to update a profile (or some other call) and get the following response from Github;
Array
(
[message] => Body should be a JSON Hash
)
I have gone over the relevant page on the API Docs: http://developer.github.com/v3/users/
And this page: http://developer.github.com/v3/#http-verbs which covers POST/PATCH
Here is the code that I'm using
$data = array("bio" => "This is my bio" );
$data_string = json_encode($data);
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME:PASSWORD");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$result = json_decode(curl('https://api.github.com/user'),true);
I have also tried CURLOPT_CUSTOMREQUEST as 'POST' and 'PATCH' but got the same error response for both.
Can anyone point me in the right direction for posting data to the API?
You have to either global $data_string or pass the $data_string variable to curl() for reusability.
Example:
function curl($curl, $data)
{
$data_string = json_encode($data);
// your code here
}
You should specify the header like this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
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"]