I have the following code
curl -v https://api.sandbox.paypal.com/v1/payments/payment \
-H 'Content-Type:application/json' \
-H 'Authorization:Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG' \
-d '{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}'
I have been trying to convert it, but I don't know which parameters to use.
How can I convert it to PHP?
Looks like you'd need to do this. Note that because you're passing a JSON header, PayPal is (likely) requiring the body data to be sent as JSON, rather than form/value pairs. If you're passing a complex set of data, you may find it easier to define $data as an array, and use json_encode($data) to transform it for the CURLOPT_POSTFIELDS property.
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Bearer EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG';
$url = 'https://api.sandbox.paypal.com/v1/payments/payment';
$data ='{
"intent":"sale",
"redirect_urls":{
"return_url":"http://example.com/your_redirect_url/",
"cancel_url":"http://example.com/your_cancel_url/"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"7.47",
"currency":"USD"
}
}
]
}';
//open connection
$ch = curl_init();
//set connection properties
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You can use json_decode to do the leg work for you.
Simply feed it the string that cURL passes back to you and it will in turn convert your JSON into a php usable array.
Related
I am trying to integrate razorpay x api for creating contacts and this is the sample api request data from razorpay. reference_id and notes are optional.
curl -u <YOUR_KEY>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/contacts \
-H "Content-Type: application/json" \
-d '{
"name":"Gaurav Kumar",
"email":"gaurav.kumar#example.com",
"contact":"9123456789",
"type":"employee",
"reference_id":"Acme Contact ID 12345",
"notes":{
"notes_key_1":"Tea, Earl Grey, Hot",
"notes_key_2":"Tea, Earl Grey… decaf."
}
}'
and below is my curl function data sent to razorpay.
$user=User::find($request->input('user_id'));
$payload= '{
"name":"'.$user->name.'",
"email":"'.$user->email.'",
"contact":"'.$user->mobile_number.'"
"type":"customer"
}';
$key="test_key";
$secret="secret_key";
$url = 'https://api.razorpay.com/v1/contacts';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_USERPWD, $key . ":" . $secret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
I am getting response as name field is required, eve though I have sent all the request fields in the request parameter. Please suggest me if my curl function is correct or not. below is the response from api call
{
"error":
{
"code": "BAD_REQUEST_ERROR",
"description": "The name field is required.",
"metadata": {},
"field": "name"
}
}
Please specify the request method:
curl_setopt($ch, CURLOPT_POST, 1);
I am working on creating div for each result coming from curl_exec json GET request. I have tried the code below using cURL in PHP. When I run this code nothing comes from the foreach.
//API Url
$url = 'https://api.clover.com:443/v3/merchants/'.$merchantid.'/categories/';
//Initiate cURL.
$ch = curl_init($url);
$headers = array(
'Content-type: application/json',
'Authorization: Bearer '.$authtoken.' ',
);
//Tell cURL that we want to send a GET request.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//Execute the request
$resp = curl_exec($ch);
$respArray = json_decode($resp, true);
foreach($respArray as $key => $value) {
echo $key . " : " . $value;
}
curl_close($ch);
This returns the following:
{
"elements": [
{
"id": "QWS8MSMAT49E6",
"name": "Other",
"sortOrder": 2
},
{
"id": "35K000MJVVFHE",
"name": "Pizza",
"sortOrder": 1
}
],
"href": "http://api.clover.com/v3/merchants/{[MID]}/categories?limit=100"
}
I'm trying to get the information above into a div, for example:
think it works like this:
$data = json_decode($resp);
foreach($data->elements as $element) {
echo '<div id="group_'.$element->id.'" class="product_group">'.$element->name.'</div>';
}
I have resolved the issue with adding
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
To the code. As has been explained Here (by Mihai)
"By default curl_exec outputs the data it gets from the server to the
standard output so your $response variable doesn't really have the
actual response data in it. If you want to get the data in a variable
set the CURLOPT_RETURNTRANSFER option before calling curl_exec."
And Martin Zeitler's Answer was right with removing true from json_decode()
I am trying to post data to create a customer using Rotessa API. I am using curl command which I've found in Rotessa API reference. But I am getting errors in return response instead of JSON string. It would be very helpful if I get help on this. Thanks
curl command:
curl -X POST -H 'Content-Type: application/json' -H "Authorization: Token
token=\"<api_key>\"" -d '{"custom_identifier": "test api", "email":
"test#rotessa.com", "name": "Mike Smith", "bank_name": "Scotiabank",
"transit_number": "11111", "institution_number": "111", "account_number":
"11111111", "address": { "address_1": "123 Main Street", "address_2": "Unit
4", "city": "Toronto", "province_code": "ON", "postal_code": "M1B 0B7" }}'
<rotessa_endpoint>/customers.json
and converted php code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.rotessa.com/v1/customers.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"custom_identifier\": \"test api\",
\"email\": \"test#rotessa.com\",
\"name\": \"Mike Smith\",
\"bank_name\": \"Scotiabank\",
\"transit_number\": \"11111\",
\"institution_number\": \"111\",
\"account_number\": \"11111111\",
\"address\": { \"address_1\": \"123 Main
Street\",
\"address_2\": \"Unit 4\",
\"city\": \"Toronto\",
\"province_code\": \"ON\",
\"postal_code\": \"M1B
0B7\" }}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Token token=\"<api_key>\"";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
it gives the following error:
{"errors":[{"error_code":"unauthorized","error_message":"Not Found."}]}
To resolve further confusion I used real api_key in my testing. Just didn't post it here.
Above you have endpoint URL /customers.json,
below you have /customers.
Normally the APIs need an API key and should be sent by curl.
curl "<rotessa_endpoint>/customers.json" -H "Authorization: Token token=\"<api_key>\""
Instead of <api_key> should enter your API key.
rotessa API KEY
I am starting to use Wit.ai to enhance a small bot I made. I am able to make a request to the wit.ai by doing:
function sendToWitAI($query){
$witRoot = "https://api.wit.ai/message?";
$witVersion = "20170822";
$witURL = $witRoot . "v=" . $witVersion . "&q=" . $query;
$ch = curl_init();
$header = array();
$header[] = "Authorization: Bearer xxxxxxxx";
curl_setopt($ch, CURLOPT_URL, $witURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
However, when receiving the output I just get the same message I sent. For example, if the user types "I want to make a reservation" my $server_output is now "I want to make a reservation" after all that chunk of code above.
Still, I know it reaches wit successfully because I can see it in the logs there and I know the bot says (from wit.ai):
{
"confidence": null
"action": null
"type": "action"
}
On top of this, if I just do a curl with the same query:
curl -XPOST 'https://api.wit.ai/converse?v=20170822&session_id=123abc&q=I%20want%20to%20make%20a%20reservation' \
> -H "Content-Type: application/json" \
> -H "Accept: application/json" \
> -H 'Authorization: Bearer xxxxxxxx'
I get the following output:
{
"confidence" : null,
"type" : "action",
"action" : null,
"entities" : {
"contact" : [ {
"suggested" : true,
"value" : "reservation",
"type" : "value",
"confidence" : 0.95062723294726
} ],
"intent" : [ {
"confidence" : 0.98638622681962,
"value" : "make_reservation"
} ]
}
}
I'm not sure where my error is or what I'm missing to properly handle use of the value like I need.
I've been googling non-stop but I can't find anything after they (wit.ai) deprecated "stories" and there's seldom anything about handling the response.
You're using 2 different end points: /message and /converse.
The log you pasted is from /converse so I'm not even sure your first call went through. Can you try a curl to /message like this
curl -XGET 'https://api.wit.ai/message?v=20170307&q=I%20want%20to%20make%20a%20reservation' \
-H 'Authorization: Bearer $TOKEN'
I'm trying to post data to a JIRA REST API but I get a blank response back. The code sits in two files. Here's the code located on the jira.json file:
{
"fields": {
"project":
{
"id": "19600"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Task"
}
}
}
and the php code:
<?php
$jsondata = file_get_contents("jira.json");
$json = json_decode($jsondata,true);
//print_r($json);
$url_send ="http://jira.greentea.co.za:8091/rest/api/2/issue/";
$str_data = json_encode($json);
//print_r($str_data);
function sendPostData($url, $post){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result)
{
return 1;
}
else{
return 0;
}
}
$time = sendPostData($url_send, $str_data);
var_dump($time);
?>
I get a zero int(0) meaning that the result variable returned a blank result. This is the the cURL Command that I converted to php. data will be replaced by the json array:
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json" http://localhost:8090/rest/api/2/issue/
If you are sure that you have CURL enabled with your PHP, try to use this snippet from my code:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json','Content-Length: ' . strlen($json_data)));
Let me know if this works for you.