Not able to send json data through api [duplicate] - php

This question already has answers here:
How to POST JSON Data With PHP cURL?
(7 answers)
Closed 5 years ago.
I am using an external api where i have been given the following details related to that api
API KEY (Mandatory Header) :- apikey : "xxxxx"
Stage Server Base Url :- https://example.com
To add Details API :- POST(/student/{student_id})
What I plan to do is:
I would be collecting data from users through codeigniter form and
then will forward the data to the above api and get a response from
the api and accordingly if its a success then would perform furthur
actions or else need to display error message
However for testing purpose i am using a static array and trying to send it to the api.
What actually is happening is I am getting a blank result and so not sure if it is working or not.
Following is my code
<?php
$apiKey = 'xxxx';
$url = 'https://www.example.com';
$data = array("name"=> "ABC","location"=> "loc","class"=> 'tenth',"area"=> "area");
$ch = curl_init( $url );
$payload = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
$headers= curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization: ' . $apiKey));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
echo "<pre>$result</pre>";
?>
PS:I am new to api and curl so a little explanation along with the solution would be appreciated

$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Authorization:Basic '.$apikey,
)
);
$result = json_decode(curl_exec($ch));
try to add whether its Basic or Bearer in authorization header. Hope it works for you.

you should use like below statement
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

Related

INVALID_TICKET in Zoho mail php Integration

I am using zoho email for sending email.
but when Trying the following code to send email ,
it returns the error invalid_ticket.
$data = array(
"fromAddress"=> "from#gmail.com",
"toAddress"=> "receive#gmail.com",
"subject"=> "Email - Always and Forever",
"content"=> "Email can never be dead. The most neutral and effective way, that can be used for one to many and two way communication.",
"askReceipt" => "yes"
);
// Convert the PHP array into a JSON format
$payload = json_encode($data);
// Initialise new cURL session
$ch = curl_init('https://mail.zoho.in/api/accounts/<user_id>/messages');
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/json',
'Content-Length: ' . strlen($payload))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
?>
Please help me to solve this. Thanks
This is not the right forum for this question but according to official documentation, you need to Kindly regenerate the authtoken with that generated authtoken which will work fine.
Source: https://help.zoho.com/portal/en/community/topic/zoho-cretor-getting-error-message-invalid-ticket-id-code-4834-when-trying-to-insert-records-in-zoho-crm

Unable to get embed token of power bi report in php after successfully generating its access token

I am using get/post request to generate embed token of the powerbi report in php and i successfully generated the access token by following the example given in this link https://community.powerbi.com/t5/Developer/How-To-Get-embed-token-using-Get-Post-only/td-p/294475 but when i used this access token to generate embed token for me it returns empty array in response.This is my code
$headers = array(
"Authorization: Bearer <acesstoken generated>"
);
$url = 'https://api.powerbi.com/v1.0/myorg/groups/<group-id>/reports/<report-id>/GenerateToken';
$post_params = array(
'accessLevel' => 'View',
'datasetId'=>'<dataset-id>'
);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo $response;
Any help would be appreciated thanks.
After the hardwork of 1 day I finally figured out i was using application id in the resource param for generating access token instead of this https://analysis.windows.net/powerbi/api link. So when i used this link ,the problem is solved and generated the embed token successfully.

Send and Receive JSON using an https curl_exec call in PHP

I'm trying to make a curl call using PHP. My project has a flow like this
I redirect user to the server.
User uses the services and the server redirects him to a page on my application.
I would like to make a server to server call at this point to verify certain details.
Point to be noted - I post JSON data as well as receive JSON data as response.
I have tried this. I am able to send data but don't get any response.
Is this correct?
Please help!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
As you mention in the comment:
The problem is with SSL verification of the host. It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) to setup SSL correctly, so a workaround is to add the following two options:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
Try to use the below code which works for me. and make sure the json_url is correct. When you run it on the browser you should get an output. Before try it using CURL, run in on the browser and see whether you get the output you want.
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);

testing gcm push notification in android

I used mark nutter phonegap plugin for GCM and I successfully get the registration id back from server
here is php script that I use
<?php
// Replace with real server API key from Google APIs
$apiKey = "AIzaSyC7wzEjjMaOGLYp8_w3USftv_3lI-qCdZ4";
// Replace with real client registration IDs
$registrationIDs = array( "APA91bFt3slFE96jaB5qnoD5gR0TCwsxe5StEGyrECR0umYviG0cfG1JNnFxYqP1ERr1RoWc38rsuWjRUx5SZ7cgUNG9-mQ4mSsY8_XQLquft5DLnqWcLCEB2wtQpfA6EAp5OQmOWzpUZU5bohfG4sfLWNUco7XxXg");
// Message to be sent
$message = "hi SOurabh";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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 );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
I do not know much about php I used thi just to test notifications
and I get following message when I run this
{"multicast_id":9187695352946100598,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
any idea why do I get this?
or any other way to test the app?
Does the project ID that you used on the project match the one shown on the google console ?
https://code.google.com/apis/console/#project:?????
Does the API key belong under 'server apps' on the google GCM page ? Your phone must also have a valid registered google account to receive notifications.
I started to use your php-script to debug push notification on my app.
First time I received the same error.
Updating SENDER_ID value to Google API Console App Project ID in Android client application fixed the issue - messages are delivered successfully.

Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401

Searching for some info about how to send notifications using GCM but with PHP instead of servlets, i found this: GCM with PHP (Google Cloud Messaging)
I tested the working code of the responses of these questions, also i created a Key for browser apps (with referers), and i give permissions to this ip: .mywebsite.com/ (te php file is on this url: "http://www.mywebsite.com/~jma/cHtml5/cap/kk.php")
But i'm getting this response: Unauthorized Error 401
What i'm doing wrong?
this is the php file:
<?php
// Replace with real server API key from Google APIs
$apiKey = "fictional key";
// Replace with real client registration IDs
$registrationIDs = array( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");
// Message to be sent
$message = "Test Notificación PHP";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'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 );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
Solved!!!
you must use Key for server apps (with IP locking) instead of browser key
:)
You need to use the key for server apps in the field of API key in your server side coding.
While creating the server key don't enter anything inside IP address field.

Categories