Error while connecting to PayPal using REST API - php

While using connect with paypal on sandbox credential during token api call i always get the err
Array ( [error] => invalid_client [error_description] => Client Authentication failed )
Below shown is the CURL call i am using. please help me out
$code = $_GET['code'];
$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_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=".$code."");
$headers = array();
$headers[] = 'Authorization: Basic client_id:client_secret';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$output = json_decode($result,true);
print_r($output);

As a comment mentions, CURLOPT_USERPWD is the simplest strategy, let curl do the work for you
If you were to be setting the Authorization header yourself, client_id:client_secret would need to be Base64 encoded
As a side note, when using curl on the command line the equivalent is the -u flag

Related

Undefined constant http/2

I am trying to work out how to send push notifications to apple iOS devices from a php site. I had a php script that was working before the new http/2 way but since they do not support it anymore, it of course does not work.
The new script I have is this:
<?php
$ch = curl_init();
$device_token = 'correctDevice';
$pem_file = 'correctPemfile';
$pem_secret = 'correctCode';
$apns_topic = 'correctAppID';
//curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
curl_setopt($ch, CURLOPT_VERBOSE , true);
echo "Try 1 ================================================" . PHP_EOL;
//setup and send first push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #1","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
echo "Try 2 ================================================" . PHP_EOL;
//setup and send second push message
$url = "https://api.development.push.apple.com/3/device/$device_token";
curl_setopt($ch, CURLOPT_URL, "{$url}");
$sample_alert = '{"aps":{"alert":"hi #2","sound":"default"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch);
//var_dump($response);
//var_dump($httpcode);
curl_close($ch);
?>
I've blocked out the device token, pem file, secret and app ID of course, but they are all correct.
The issue with it is that I get this error:
Warning: Use of undefined constant CURL_HTTP_VERSION_2_0 - assumed 'CURL_HTTP_VERSION_2_0' (this will throw an Error in a future version of PHP)
What could this issue be? I tried echoing phpinfo() and see if it supports http/2 and I found this:
From the documentation:
CURL_HTTP_VERSION_2_0 (int)
Available since cURL 7.33.0
Check the cURL version (e.g. by using phpinfo()).

Failing to post json to an endpoint using curl

Im trying to create a simple api that will post json to an endpoint using curl and php. its giving me an error whereby its saying unsupported media type and i need help seeing what it is i might be doing wrong, here is the code
<?php
$data = array("saleAmount"=>"2000","cashBack"=>"800","posUser"=>"John","tenderType"=>"SWIPE","currency"=>"RTGS","transactionId"=>'0001');
$payload = json_encode($data);
echo $payload;
// prepare curl
$ch = curl_init('http://localhost:9111/api/requests');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/jsonp',
'Content-Length: ' . strlen($payload))
);
// Submit the POST request
$result = curl_exec($ch);
echo json_encode($result);
// Close cURL session handle
curl_close($ch);
?>
The issue is that your API is expecting a different media type than the one you are providing in your quest.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415
Make sure that your API supports the content-type application/json
$url="";//path of api
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')
);
$data = array("saleAmount"=>"2000","cashBack"=>"800","posUser"=>"John","tenderType"=>"SWIPE","currency"=>"RTGS","transactionId"=>'0001');
$data=json_encode($data);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$response=curl_exec($ch);
print_r($response);die;
curl_close($ch);
Append your url in $url and check this once.

How to fix curl error "Could not resolve host: account-d.docusign.com%2Foauth%2Ftoken" in Docusign API in php

I am following below link:
https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-code-grant
Step 1 worked fine and I am able to receive Authorization Code
Step 2 Not working gives error "Could not resolve host: account-d.docusign.com%2Foauth%2Ftoken"
Running this on Apache server, php 7.3, using CURL.
$authcode = "";
if(isset($_GET['code'])){
$authcode = $_GET['code'];
}
$headauth = base64_encode('bbc034da-a19f-48db-a841-25832e209a41:2f08bab1-2ccc-435f-a1f7-d43802b51255');
$request_headers = array();
$request_headers[] = 'Content-Type: application/x-www-form-urlencoded';
$request_headers[] = 'Authorization: Basic '.$headauth;
$ch = curl_init();
$url = str_replace ( ' ', '%20', "https://account-d.docusign.com/oauth/token" );
curl_setopt($ch, CURLOPT_URL,urlencode($url));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=authorization_code&code=".$authcode."&redirect_uri=http://192.168.2.56/testdocusign/test.php"); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$server_output = curl_exec ($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
print_r($error_msg);
}
curl_close ($ch);
print_r($server_output) ;
I expect result something like below:
{
"access_token": "ISSUED_ACCESS_TOKEN",
"token_type": "Bearer",
"refresh_token": "ISSUED_REFRESH_TOKEN",
"expires_in": 28800
}
But my current error is :
Could not resolve host: account-d.docusign.com%2Foauth%2Ftoken
As you can see the url is incorrect. you need to get the access code that you got from the first call you said worked ok and add it to the second call instead of the %2Foauth%2Ftoken part which is obviously just a placeholder for the token.
(edit: the issue was incorrect url encoding, that was the culprit)

OAuth1.0 with php and CURL not working

I am trying to hit api which is having OAuth1.0.
Problem is when i am trying with Postman i am getting response but with PHP curl its giving error.
In postman i am passing parameters as below
url='xxxx', Authentication stuff is type=OAuth1.0,Consumer Key,Consumer Secret, Token, Token Secret, Timestamp, Nonce, Version=1.0, Realm=Optional
Below is my code which i have tried
$url="xxx";
$header[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode('Authorization:OAuth oauth_consumer_key="xxx",oauth_token="xxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1475651061",oauth_nonce="LyA5sR",oauth_version="1.0",oauth_signature="xxx"'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
echo "<pre>";print_r($result);exit;
I have added CURLOPT_SSL_VERIFYPEER, because there was certification problem. Now i am getting 'Access denied' from api curl error 403 because there is something wrong in the way i am passing for Oauth 1.0 by curl.
This is the first time i am trying outh1.0 with curl. Please help me whats going on in my code. I have tried using this link
The issue is with oauth signature which is not getting formed properly. You can refer https://oauth.net/core/1.0/ for more clarity

Passing an OAuth token to the Yahoo API

I have created a new app using the Yahoo API. How can I pass the required headers using CURL functionality? I got this error message when I tried:
<yahoo:error xml:lang="en-US"><yahoo:description>Please provide valid credentials. OAuth oauth_problem="unable_to_determine_oauth_type", realm="yahooapis.com"</yahoo:description></yahoo:error>
How can I pass the required headers in this code:
$url ="http://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1";
$ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
//get the url contents
$data = curl_exec($ch);
//execute curl request curl_close($ch);
$xml = simplexml_load_string($data);
print_r($xml);
exit;
Once you'd obtained an OAuth 2.0 access token, use it in the following code:
$token = "<token>";
$url = "https://fantasysports.yahooapis.com/fantasy/v2/team/223.l.431.t.1?format=json";
$headers = array(
'Authorization: Bearer ' . $token,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response);
print_r($json);
Notice that it presents the token in an Authorization HTTP header over a secure transport channel using the https URL scheme and requests to return the content as JSON using the format URL query parameter.

Categories