Paypal - Subscriptions - Create a product - php

I am trying to make subscriptions integration with paypal.
I am following the instructions from here:
https://developer.paypal.com/docs/subscriptions/integrate/#
I have a problem in step number 2 (Create a product).
I am using php to make curl call but I get error and can't solve it. The curl link is: https://api.sandbox.paypal.com/v1/catalogs/products
The response I got is:
{
"name": "NOT_AUTHORIZED",
"message": "Authorization failed due to insufficient permissions.",
"debug_id": "7de3b61dcde85",
"details": [
{
"issue": "PERMISSION_DENIED",
"description": "You do not have permission to access or perform operations on this resource"
}
],
"links": [
{
"href": "https://developer.paypal.com/docs/api/v1/billing/subscriptions#NOT_AUTHORIZED",
"rel": "information_link",
"method": "GET"
}
]
}
Someone know please how can I fix it? How I can add permission so I can create a product?

First, you have to access token to authorize the permission
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'your client id' .':'. 'your secret Key');
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Accept-Language: en_US';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$accessToken json_decode($result);
after getting access token, you send another hit to create a product
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/catalogs/products');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "Test Recurring","type": "SERVICE}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
$headers[] = 'Paypal-Request-Id: <your client id>';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$createProduct = json_decode($result);
here your product creation is complete and you get a product id
Ref1: getAccessToken
Ref2: CreateProduct

Related

unable to use api using curl in php

i am working on linkedin api in php and trying to post "comment" on "linkedin post" (dynamic), I tried with following code but not working,I am getting following error
{"message":"java.net.URISyntaxException: Illegal character in path at index 20: /rest/socialActions/{shareUrn|ugcPostUrn|commentUrn}/comments"}
I tried with following code but not working,Kindly tell me which type of parameter should i pass(use) ?
what is "{shareUrn|ugcPostUrn|commentUrn}/comments" which type of data should i use
which parameter should use for "actor","object"
Here is my complete code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.linkedin.com/rest/socialActions/{shareUrn|ugcPostUrn|commentUrn}/comments');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"actor\":\"urn:li:person:A8xe03Qt10\",\n \"object\":\"urn:li:activity:6631349431612559360\",\n \"message\":{\n \"text\":\"commentV2 with image entity\"\n },\n \"content\":[\n {\n \"entity\":{\n \"digitalmediaAsset\":\"urn:li:digitalmediaAsset:C552CAQGu16obsGZENQ\"\n },\n \"type\":\"IMAGE\"\n }\n ]\n}");
$headers = array();
$headers[] = 'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxx';
$headers[] = 'Linkedin-Version: 202208';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_R($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

where to add API Key to the Curl in php

Am trying to leverage the Anvil API for PDF.
Here is their sample request.
curl \
-X POST \
-u YOUR_API_KEY: \
-H 'Content-Type: application/json' \
-d '{ "data": { "someKey": "some data" } }' \
https://app.useanvil.com/api/v1/fill/{pdfTemplateID}.pdf > test.pdf
My problem is where to add the API KEY. I have tried adding it to the header but it throws error {"name":"AuthorizationError","message":"Not logged in."}
Here is the coding so far
$url2="https://app.useanvil.com/api/v1/fill/first.pdf";
$ch2 = curl_init();
curl_setopt($ch2,CURLOPT_URL, $url2);
$apiKey ='my api key goes here';
$post_data ='
{
"data": {
"someName": "Bobby",
"someDate": "2018-10-31",
"anAddress": {
"street1": "123 Main St",
"city": "San Francisco",
"state": "CA",
"zip": "94106"
}
}
}';
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
//'Content-Type:application/json'
'Authorization: ' . $apiKey
));
curl_setopt($ch2,CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch2,CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch2,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch2,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch2,CURLOPT_RETURNTRANSFER, true);
echo $response2 = curl_exec($ch2);
curl_close($ch2);
The curl command you provided has option -u, which is expecting data as username:password ,from curl man
-u/--user user:password Specify user and password to use for server authentication. If this option is used several times, the last one
will be used.
which in PHP you have to send headers like below snippet:
CURLOPT_HTTPHEADER => [
'Authorization: Basic ' . $apiKey . ':'
],
or with
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
related thread
Edit: from your link in comment, they are expecting raw data of your request which you can accomplish by sending it as put request:
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT');
or with text/plain header
**
- ***UPDATED***
**
are you encoding the API key as "base64" ??
$YOUR_API_KEY = base64_encode("YOUR_API_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://app.useanvil.com/api/v1/fill/XnuTZKVZg1Mljsu999od.pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"title\": \"Hello\", \"data\": [ { \"label\": \"Hello World\", \"content\": \"I like turtles\" } ] }");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $YOUR_API_KEY . ':' . '');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Easiest way to do add api key in curl request is as follow
// Collection object
$ch = curl_init($url);
$headers = array(
"APIKEY: PUT_HERE_API_KEY",
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml"
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
$result = curl_exec($ch); // execute
$result;
//show response
curl_close($ch);
curl -X GET -k -H 'Content-Type: application/json' -H 'X-ApiKey : YOUR_APIKEYHERE' -i 'YOUR API RNDPOINT URL HERE'
X-ApiKey is the name of your API key.

PHP PayPal Catalog REST API will not display new product

Since adding a couple of new sandbox Products using the Catalog API with the PHP curl object, this is what has happened:
The two products are visible using the REST API.
But I can only edit the second one using PATCH. The calls to update the other one do not error, they just don't do anything.
I can add new Products using the API, again with no errors but they do not appear in the list API response afterwards.
When trying to add a new Product with the same ID as one I apparently added before but which is not visible, there is a DUPLICATE_RESOURCE_IDENTIFIER error in the response array, confirming that it is in there somewhere.
What on earth is going on?
Here is the code:
ADD PRODUCT
$ch = curl_init();
$authorization="Authorization: Bearer ".$authorization;
$url=$sbx=='1'?$url='https://api.sandbox.paypal.com/v1/catalogs/products':'https://api.paypal.com/v1/catalogs/products';
echo $url."<br><br>";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "xxxxxx","description": "App 005", "type": "SERVICE", "category": "SOFTWARE", "id": "RBW00005"}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = $authorization;
$headers[] = 'Paypal-Request-Id: FGAS005';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$response = json_decode($result);
print_r($response);
echo "<br><br>";
print_r(curl_getinfo($ch));
$response="";
curl_close($ch);
LIST PRODUCTS
$ch = curl_init();
$authorization="Authorization: Bearer ".$authorization;
$url=$sbx=='1'?$url='https://api.sandbox.paypal.com/v1/catalogs/products?page_size=2&page=1&total_required=true':
'https://api.paypal.com/v1/catalogs/products?page_size=2&page=1&total_required=true';
echo $url."<br><br>";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = $authorization;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$response = json_decode($result);
print_r($response);
echo "<br><br>";
print_r(curl_getinfo($ch));
$response="";
curl_close($ch);
And it's not missing square brackets from around the post string.
If I put them in I get a 'malformed request' error. No square brackets, no error but no visible Product either.
Thanks for reading.
Just noticed you seem to be hardcoding a Request ID header:
$headers[] = 'Paypal-Request-Id: FGAS005';
Ensure you never do this; the value of that header ought to be unique for each independent request, or more simply not set at all (comment this line out)

Php cURL with calling an API with GRAPHQL

I am trying to call an api called Wave I have used cURL before but never with GRAPHQL queries. I am wondering what is wrong with the below when using cURL. I get an error Bad Request Below is an exmple of my code.
This is what the API cURL is
curl -X POST "https://reef.waveapps.com/graphql/public" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-H "Content-Type: application/json" \
-d '{ "query": "query { user { id defaultEmail } }" }'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://reef.waveapps.com/graphql/public');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "query": "query { user { id defaultEmail } }');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 1212121';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
Any help would be helpful.
For those wanting to query a GraphQL service WITHOUT a third party library, I basically took Brian's code and tested against a GraphCMS service I had already written Node.js code for. So I knew the url, authorization token, and query all worked.
<?php
$endpoint = "https://api-euwest.graphcms.com/v1/[[your id number here]]/master";//this is provided by graphcms
$authToken = "[[your auth token]]";//this is provided by graphcms
$qry = '{"query":"query {products(where:{status:PUBLISHED}){title,img,description,costPrice,sellPrice,quantity,sku,categories {name},brand {name}}}"}';
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
?>
All worked fine.
The auth token is a big long character string provided by GraphCMS and only needs to be passed in the header. So no real tricky authentication process - as long as you have the token.
I can recommend using https://github.com/softonic/graphql-client, it has worked great for us.
A way easier way to go about doing this is by using an API platform. I often use Postman, the platform have the functionality to give you the PHP cURL code for a GraphQL request in the GraphQl tools part of the application.
You can create your own client passing whatever middleware you'd like:
$clientWithMiddleware = \MyGuzzleClientWithMiddlware::build();
$graphQLClient = new \Softonic\GraphQL\Client(
$clientWithMiddleware,
new \Softonic\GraphQL\ResponseBuilder()
);
For an example how to build a Guzzle client with middleware you can check this out:
https://github.com/softonic/guzzle-oauth2-middleware/blob/master/src/ClientBuilder.php
If there is no authentication
You can use file_get_contents instead of curl
$url = http://myapi/graphql?query={me{name}}
$html =file_get_contents($url);
echo $html;
use json in query paramter for graphql;
Bit late but I made this code
$endpoint = "https://gql.waveapps.com/graphql/public";
$authToken = ""; //Your Bearer code
$qry = '{"query": "query {user {id firstName lastName defaultEmail createdAt modifiedAt}}"}';
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
var_dump($result);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

Using CURL to return JSON from API in PHP

How can I GET the contents of this API using the given method of CURL?
curl -X GET --header 'Accept: application/json' 'https://api.rezdy.com/v1/products?limit=2&offset=0'
It should give a response of
{
"requestStatus": {
"success": false,
"error": {
"errorCode": "4",
"errorMessage": "Missing API Key"
}
}
}
All attempts at using render the page empty and other methods of extracting the data from the API such as JS have a CORS issue.
Try this PHP code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.rezdy.com/v1/products?limit=2&offset=0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Output :
{
"requestStatus":{
"success":false,
"error":{
"errorCode":"4",
"errorMessage":"Missing API Key"
}
}
}

Categories