Hi,
I have setup a business account at Paypal, and it seems that I already have my Test API Credentials, since I can successfully retrieve them from my Sandbox accounts.
Now I am trying to make the "first call" to get an authorization token.
Here's my PHP code:
$ch = curl_init();
$clientId = "myid"; //not the actual one
$secret = "mypass"; //not the actual one
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, "Accept: application/json");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
$json = json_decode($result);
print_r($json);
curl_close($ch);
And here's the response:
stdClass Object ( [error] => invalid_client [error_description] => Invalid client credentials )
Does anybody have a clue?
Thanks
EDIT: This login is working (I found it elsewhere):
clientId: ASF6RRBP0uTq7FnC90tpFx7vfA-Pliw8uQDjv5RZ10Y_NVspuc88pUPLN6yM
Secret: EAdx7BDKzWczDomYG2QDHu8jhaAXj4xDZLHadvL5aRfesjwo5c81zbSpRxuE
Strangely the format looks very different from mine.
I'm stuck here, cannot go any further without a token.
You could pass it as a header similar as follows:
"Authorization" => "Basic " . base64_encode($clientId . ":" . $clientSecret)
This should act the same as above.
You could write it on curl_setopt like this:
curl_setopt($ch, CURLOPT_HEADER, "Authorization: Basic " . base64_encode($clientId . ":" . $clientSecret));
Also, just out of curiosity, you might want to try out PayPal-PHP-SDK. They have all these bells and whistles, that would keep you away from manually getting this code. It has documentations, and samples, that would help you get started with any API fairly quickly.
finally i managed to get it to work
the correct credentials are a hash of 60-digit, not 16 as first seen
i had to create an app to get them
nobody told me that at paypal, not even the "merchant technical support"
hope this helps someone in the future
thanks for your answers
Related
I have an API in postman. I want to create a CURL Request and get proper response with it. This is my POSTMAN API.
I am successfully getting this response with it.
"{\"Request\":{\"state\":\"Manama\",\"address\":\"406 Falcon Tower\",\"address2\":\"Diplomatic Area\",\"city\":\"Manama\",\"country\":\"BH\",\"fullname\":\"Dawar Khan\",\"postal\":\"317\"},\"Response\":{\"status\":\"Success\",\"code\":100,\"message\":\"Address is verified\"}}"
Now I want to use this API Call inside my PHP Code. I used this code.
$data = array(
'Request' => 'ValidateAddress',
'address' => test_input($form_data->address),
'secondAddress' => test_input($form_data->secondAddress),
'city' => test_input($form_data->city),
'country' => test_input($form_data->country),
'name' => test_input($form_data->name),
'zipCode' => test_input($form_data->zipCode),
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$data_string = json_encode($data);
$url = 'myurl.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
echo '<pre>';print_r($json_result);echo '</pre>';
But I can't see my $json_result. It just echoes <pre></pre> in the view. Can anyone guide me? Thanks in advance. I want to get my Response.
UPDATE
I used curl_error and it gives me the following error.
Curl error: SSL certificate problem: self signed certificate in certificate chain
It is Very Simple Just Click on Code you will get the code in php.
you will get the code in many language like php,java,ruby,javascript,nodejs,shell,swift,pythom,C# etc.
Answer updated as per updated question.
There are two ways to solve this issue
Lengthy, time-consuming yet clean
Visit URL in web browser.
Open Security details.
Export certificate.
Change cURL options accordingly.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");
Quick but dirty
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
We are configuring cURL to accept any server(peer) certificate. This isn’t optimal from a security point of view.
Excerpt from very detailed and precise article with screenshots for better understanding. Kindly refer the same before actually implementing it in production site.
I am relatively new to PHP and the wonderful world of API's. To set the scenario for you, we have 12 online stores all on different domains, some on different servers, all localised to local languages etc. All of them are running Wordpress with Woocommerce 2.3.13.
I am currently trying to make a 'web app' type of dashboard that calls Woocommerce data via API and displays it in a friendly manner from all of the stores. I am using PHP/HTML to display the data, I have so far managed to get the orders count for each site without issue. However now I am looking to delve a little more in to the reporting and get some sales figures and various other bits.
To achieve the order count display I have used the following code:
<?php
$data = array();
$data_string = json_encode($data);
$username = 'ck_111111111111'; // Add your own Consumer Key here
$password = 'cs_111111111111'; // Add your own Consumer Secret here
$ch = curl_init('https://example.com/wc-api/v2/orders/count/?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result,true);
echo $result['count'];
curl_close($ch);
?>
This displays the order count for that particular woocommerce store. No problem.
But now I would really like to get sales reports, I have tried adding in the date filters on the url, but when putting the date filter on the end of url like this
$ch = curl_init('https://tinywaist.co.uk/wc-api/v2/orders/count/sales?filter[date_min]=2015-01-18&filter[date_max]=2015-01-21?consumer_key='.$username.'&consumer_secret='.$password);
I don't get anything back at all.
I have been trying various manipulations on the above code to achieve what I need, I have searched high and low to find a solution to this. What I am really not grasping is how to call specific data from the Woocommerce API and display it on the front end as an array, orders count is as far as I have got.
I have read, re read and re read again the Woocommerce REST API docs, but just cannot seem understand how to apply what I am reading in to a curl command within the PHP.
If you need any more info to help please just ask, I am bald, but if I had hair, I would be tearing it out!
Thanks in advance!
From this tutorial it appears that date_min and date_max are not the correct filters. The tutorial says this is the correct approach for getting the orders in January 2014.
$ curl
https://www.skyverge.com/wc-api/v1/orders?filter[created_at_min]=2014-01-01&filter[created_at_max]=2014-01-31
For anyone looking for the answer to my question I will repost the CURL request I am using.
The documentation on Woocommerce REST API is pretty poor and lacking in quantity!
$data = array();
$data_string = json_encode($data);
$username = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init('https://example.com/wc-api/v2/orders/?filter[period]=year&filter[status]=processing&consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
I am now having issues with parsing the JSON data coming out, but that is another story!
Please excuse my terminology if I get anything wrong, I'm new to Google API
I'm trying to add events via the Places API, for a single venue (listed under the bar category). I've followed the instructions here:
https://developers.google.com/places/documentation/actions?utm_source=welovemapsdevelopers&utm_campaign=places-events-screencast#event_intro
and this is the URL I am posting to (via PHP)
https://maps.googleapis.com/maps/api/place/event/add/format?sensor=false&key=AIzaSyAFd-ivmfNRDanJ40pWsgP1 (key altered)
which returns a 404 error. If I have understood correctly, I have set the sensor to false as I am not mobile, and created an API key in Google apis, with the PLACES service turned on.
Have I missed a vital step here, or would a subsequent error in the POST submission cause a 404 error? I can paste my code in but I thought I'd start with the basics.
Many thanks for your help, advice and time. It's very much apprecciated.
I've added this line to my CurlCall function which I believe should specify a POST, and the result is still the same.
curl_setopt($ch, CURLOPT_POST, 1);
so the whole functions reads
function CurlCall($url,$topost)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
$body = curl_exec($ch);
curl_close($ch);
echo $body;
echo '<br>';
echo 'URL ' . $url;
echo '<br>';
return $body;
}
Are you sure that you are using the POST method and not GET?
You need to specify the format of your request in the URL by changing the word format to either json or xml depending on how you are going to structure and POST your request.
XML:
https://maps.googleapis.com/maps/api/place/event/add/xml?sensor=false&key=your_api_key
JSON:
https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=your_api_key
I am using the box api as mentioned in the documentation. I get the ticket from api_key and then auth_token from api_key and ticket.
But when i use this auth_token for getting information like folder list or file contents etc. I get unauthorized error in response.
Here is my code:
$auth_token = $_REQUEST["auth_token"];
//exit($auth_token);
$headers = array(
"Authorization: BoxAuth api_key=".$box_api_key."&auth_token=".$auth_token
);
$http_message = curl_init("https://www.box.com/api/2.0/folders/0");
curl_setopt($http_message, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http_message, CURLOPT_HTTP_VERSION, "CURL_HTTP_VERSION_1_1");
curl_setopt($http_message, CURLOPT_HTTPHEADER, $headers);
curl_setopt($http_message, CURLINFO_HEADER, true);
curl_setopt($http_message, CURLINFO_HEADER_OUT, true);
curl_setopt($http_message, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($http_message);
print($response);
What may be the problem in the code?
Thanks!
Thank you very much in advance!!!
Make sure you're setting $box_api_key to the api_key you're using to get the ticket and auth token. Also, while this should have no bearing on your question, please use the hostname api.box.com for all API V2 requests (aka, https://api.box.com/2.0/folders/0)
In PHP, I'm using curl to send a delete to the fb graph api - and yet I'm getting the following error;
{"error":{"type":"GraphMethodException","message":"Unsupported delete request."}}
The code I'm using is;
$ch = curl_init("https://graph.facebook.com/" . $status_id . "");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CAINFO, NULL);
curl_setopt($ch, CURLOPT_CAPATH, NULL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$result = curl_exec($ch);
echo $result;
$query contains the access token.
Fixed!
You have to prepend the userid to the object ID when deleting:
DELETE https://graph.facebook.com/673509687_104812882909249?access_token={access_token}
where
673509687 is my userID and 104812882909249 is the objectID
For anyone still struggling with this, I found out what my issue was attempting to delete application requests that I had previously created using the PHP SDK, which was resulting in this error.
(#2) Invalid parameter: Body of an error/warning message. Title is: Invalid parameter
The problem was essentially with which access token was being used; user or application.
The specific scenario I was working on was where a user in my application has invited a friend Facebook (using an app request) but then wants to revoke that invite. In this case I want to delete the app request on Facebook that was previously created. However, at this point in time, the logged in user is not the recipient of the app request, but the sender.
Looking at the PHP SDK code, it automatically uses the user access token if it has one, over the application access token. In fact, there doesn't appear to be a way to explicitly get the application token from the SDK.
When attempting to delete the app request using the following...
$facebook->api('/'.$fb_request_id, 'DELETE');
...and letting the PHP SDK choose the user token, I received the (#2) Invalid parameter error message. However, if I manually construct the application access token (where the format is "$app_id|$app_secret" and pass it as an array key in a third parameter...
$facebook->api('/'.$fb_request_id, 'DELETE', array('access_token' => $app_access_token);
..then the call succeeds.
So, essentially you need to use the application access token to delete the app requests if the current user is not the recipient of the app request.
I hope this helps anyone else struggling with the same issue.
I modified your code slightly. (Should echo "true" if done correctly) Here's what is currently working for me.
Also note this does not erase events created via Facebook.That's why your receiving the permissions error. This only erases events created through your application... (application linked to $app_id, $app_secret)
//First authenticate a token
$app_id = "APP ID GOES HERE";
$app_secret = "SECRET APP ID GOES HERE";
$my_url = "WHATEVER THIS PAGES NAME IS GOES HERE";
//I'm not sure but I think REQUEST is still allowed....right? if not change it to GET/POST
$code = $_REQUEST["code"];
if(empty($code)) {
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=create_event";
echo("<script>top.location.href='" . $auth_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
//Use TRUE and FALSE not 0 and 1's like you originally had it
//264853420218553 is the event id.
$ch = curl_init("https://graph.facebook.com/264853420218553?" . $access_token . "");
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, TRUE);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CAINFO, NULL);
curl_setopt($ch, CURLOPT_CAPATH, NULL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
$result = curl_exec($ch);
echo $result;?>
The only thing I can think to try is to
1) do a POST request with "method=delete" to see if that works
2) manually look at the produced HTTP request to see if something looks wrong -- then you can isolate the problem
It simply means that the HTTP delete method isn't supported for that specific object.
One option is to use Http POST and add method=delete to the parameter query. Make sure that your application has a publish_stream permission else you can never publish a feed.
Permissions are done by Facebook.
About this answer:
Fixed!
You have to prepend the userid to the object ID when deleting:
DELETE https://graph.facebook.com/673509687_104812882909249?access_token={access_token}
where 673509687 is my userID and 104812882909249 is the objectID
Unfortunately this will only work with a user-accesstoken and not when you try to delete an apprequest on the server (with the app-access-token) using for example the PHP language.
If you have a solution for deleting apprequests using the app-access-token then please describe it. Thanks for your help!