Razorpay x Api call BAD_REQUEST_ERROR using Laravel - php

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);

Related

cURL POST : how to send data as BODY of the request (not POST param)

I need to contact a WebService:
this WS accepts only POST.
For authenticating I have to send some JSON in the BODY of request
while in the HEADER I have to send the WS method I want to call.
This is a valid request sent using CLI (WS answers correctly)
curl -X POST -k -H 'Operation: TPLGetCardData' -H 'card_num: 123456789' -i 'https://example.com/ws.aspx' --data '{
"auth": [
{
"Timestamp": 1669910083,
"SenderIdentifier": "XXX-XXX-XXXX",
"ConnectionKey": "XXXX"
}
]
}'
This is the PHP code I've written, but I receive an error from the WS
$data = '{
"auth": [
{
"Timestamp": 1669910083,
"SenderIdentifier": "XXX-XXX-XXXX",
"ConnectionKey": "XXXX"
}
]
}';
$cURLConnection = curl_init();
curl_setopt($cURLConnection, CURLOPT_URL, 'https://example.com/ws.aspx');
curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURLConnection, CURLOPT_POST, true);
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($data));
//curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data);
curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array('Operation: TPLGetCardData', 'card_num: 123456789'));
//curl_setopt($cURLConnection, CURLOPT_VERBOSE , true);
$result = curl_exec($cURLConnection);
curl_close($cURLConnection);
$jsonArrayResponse - json_decode($result);
print_r('RESULT is <pre>'.$result.'</pre>');
If I send the request with
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data)
the error is "no credentials"
if I send the request with
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($data));
the error is "wrong credentials"
I don't understand which is the difference between what I send with curl CLI command and what I send with PHP.
If someone could help me, it will be really apreciated
:::EDIT:::
Sorry, it came out that the problem was on the WS side, my request was OK...2 days lost in finding a non existing problem.
Try this by encoding the data before passing it to curl
$data = json_encode(array(
"Timestamp" => "1669910083",
"SenderIdentifier" => "XXX-XXX-XXXX",
"ConnectionKey" => "XXXX"
"ShortCode" => "600981"
));
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data);
I apologize to everyone who helped, but it came out that the problem was on the WS side, my request was OK...
2 days lost in finding a non existing problem.
Sorry

I m looking to get bitly clicks using it api v4

I am trying to get clicks per link from : https://api-ssl.bitly.com/v4/bitlinks/{bitlink}/clicks/summary i am not getting any result and getting 404 instead of getting clicks back. I am using v4 api of bitly.
I have tried through curl but i m not getting anything.
$url = 'https://api-ssl.bitly.com/v4/bitlinks';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['long_url' => "https://booktennislessons.com/app/Frontend/registerProgram/165?data="]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $code",
"Content-Type: application/json"
]);
$arr_result = json_decode(curl_exec($ch));
echo $arr_result->link;
echo "<pre>";
print_r($arr_result);
this code works fine for getting bitly but not getting the clicks from it. its a post request and i need to send a get request .
i m getting 404 but it should comes up with
{
"units": 0,
"unit_reference": "string",
"unit": "minute",
"link_clicks": [
{}
]
}```

cURL command to PHP code returns error

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

JSON data to REST API using cURL give a blank response

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.

Not able to send notification to a group using notification_key even though it is possible through the regId

I made some php code and I was able to send notifications to devices using the regId.
I also managed to create a group of regId successfully receiving the notification_key as describe on the google documentation.
But I'm not able to send notifications to the group. I tried to use the same method to send the notification but instead of using the regId I am using the notification_key I received from GCM when creating the group, but this approach did not work, it gives me the NotRegistered error.
If i try to register with the same notification_key_name GCM say it is already registered.
I am not sure if I have to send it through another method or if I am doing something wrong.
When I send the notifications using the regId I receive this message
from GCM:
{"multicast_id":517...442,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:140...ecd"},{"message_id":"0:140...ecd"}]}
When I create the group I receive this message from GCM:
{"notification_key":"APA91....nz9Q"}
When I try to send the message to the group using the notification_key i received on the message above I receive this message from GCM:
{"multicast_id":80...63,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
When I try to create the group again with the same notification_key_name I receive this message from GCM:
{"error":"notification_key already exists"}
Below is the code I am using.
<?php
class GCM {
const GOOGLE_API_KEY= " *** MY API KEY ***"; // Place your Google API Key
const PROJECT_KEY= " *** MY PROJECT KEY ***";
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message
);
$headers = array(
'Authorization: key=' . self::GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
public function requestNotificationKeyFromGCM($registatoin_ids, $username) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/notification';
$request = array(
'operation' => 'create',
'notification_key_name' => $username,
'registration_ids' => $registatoin_ids,
);
$headers = array(
'Authorization: key=' . self::GOOGLE_API_KEY,
'project_id: ' . self::PROJECT_KEY,
'content-type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
}
?>
I had similar issues while trying to send a gcm's with notification key. There is an issue in the documentation.
Instead of using:
curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data #- "https://android.googleapis.com/gcm/send" << EOF
{
"registration_ids": ["<REGISTRATION-ID-FROM-DEVICE>"],
"data": {},
}
EOF
You have to use:
curl -vvv -X POST --header "Content-Type: application/json" --header "project_id: <YOUR-PROJECT-ID>" --header "Authorization: key=<YOUR-PROJECT-SECRET-KEY>" --data #- "https://android.googleapis.com/gcm/send" << EOF
{
"to": "<NOTIFICATION-ID>",
"data": {},
}
EOF
You can see more in my blog post: https://medium.com/appunite-edu-collection/d7df385b0ff4
I followed the documentation from Google's Documentation
(I'm pushing to both iOS and Android).
{
"to": "your_token_ID",
"data": {
"hello": "This is a Firebase Cloud Messaging Device Group Message!",
}
}
But it's not working so I tried:
{
"to": "your_token_ID",
"notification": {
"sound": "default",
"gcmSandbox": "true",
"badge": 1,
"title" : "Push Title",
"body": "Push Body"
}
}
This method is working for me, hope it helps somebody else.

Categories