I am getting blank json in response when appOpen event is called
My code : -
$headers = array(
"X-Parse-Application-Id:trPYPHrI1Cfb8Q4w3TQ5HY7RoDcOFSM4okdNWDyC" ,
"X-Parse-REST-API-Key:OeNTANdx5A6ILHIZi3NfVcuE3GPDg4Cmyo9aQM1R",
"Content-Type: application/json"
);
$data='{ }';
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,"https://api.parse.com/1/events/AppOpened");
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest, CURLOPT_POST,true);
curl_setopt($rest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($rest);
echo $response;
curl_close($rest);
output : { }
working url : http://norefresh.thesparxitsolutions.com/rajeevranjan/parse/appopen.php
please help.
This URL https://api.parse.com/1/events/AppOpened
return {} or true means your app lauched. You can check responsecode by
$responseCode = curl_getinfo($rest, CURLINFO_HTTP_CODE);
echo "<pre>";
print_r($responseCode);
echo "</pre>";
I have checked with other url https://api.parse.com/1/classes/Player, as it's working fine. check following code:
$headers = array(
"X-Parse-Application-Id:trPYPHrI1Cfb8Q4w3TQ5HY7RoDcOFSM4okdNWDyC" ,
"X-Parse-REST-API-Key:OeNTANdx5A6ILHIZi3NfVcuE3GPDg4Cmyo9aQM1R",
"Content-Type: application/json"
);
$data='{ }';
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,"https://api.parse.com/1/classes/Player");
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest, CURLOPT_POST,true);
curl_setopt($rest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($rest, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($rest);
echo "<pre>";
print_r($response);
echo "</pre>";
curl_close($rest);
Related
I'm trying to validate api data with POST request using cURL but getting no response.
API documentation
<?php
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$data = array(
"Parameters" => array(
"apiKey" => "XXXXXX",
"id" => "9346",
)
);
$encoded = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$decoded = json_decode($resp);
print_r($decoded);
curl_close($ch);
?>
Does anyone know what is wrong?
SOLUTION:
Turns out i was missing CURL_HTTPHEADER.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Accept: application/json"
));
Try to write:
$ch = curl_init();
instead of :
$ch = curl_init($url);
Eventualy you can use a try ... catch to get the error:
<?php
// Define variables
define('API_KEY', 'XXXXXX');
$url = "https://widget.packeta.com/v6/api/pps/api/widget/validate";
$id = "9346";
// Prepare data
$data = array(
"Parameters" => array(
"apiKey" => API_KEY,
"id" => $id,
)
);
$encoded = json_encode($data);
try {
// Initialize cURL
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL request
$resp = curl_exec($ch);
if($resp === false) {
throw new Exception(curl_error($ch));
}
// Decode response and print it
$decoded = json_decode($resp);
print_r($decoded);
// Close cURL session
curl_close($ch);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
How to extract the "SALECODE" and "PRICE" from this curl get request in php for later MySql compare and insert if it's different, and what type of JSON is the result?
?php
$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url); curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);
//print_r($result);
$json = json_decode($result, true); print_r($json);
echo $json["SALECODE"];
?>
Sample Response:
{
"result":[
{
"DATASET":[
{
"SUPP_UM":"Kg",
"DETAILS":"70025",
"PRICE":"1.5",
"REC":1,
"CATEG_10":"-",
"NOTES":"",
"LASTSUPPLIER":"Furnizor 2",
"CATEG_9":"-",
"CATEG_8":"-",
"CATEG_7":"-",
"CATEG_6":"-",
"CATEG_5":"-",
"CATEG_4":"-",
"CATEG_3":"-",
"CATEG_2":"Paine",
"CATEG_1":"Panificatie",
"DIVISIBLE":0,
"SALECODE":"12",
"VAT_LETTER":"B",
"ISALT_PRICE":0,
"VAT_VALUE":"9",
"LISTED":1,
"LASTSUPPLY_UNIT_COST":"4",
"SUPP_UM_RATIO":"3.33",
"IDEXTAPP":"70025",
"IDSMARTCASH":1,
"DESCRIPTION":"12",
"LASTSUPPLY_DATE":"2015.01.05 00:00:00",
"SALE_UM":"Buc",
"IDSMARTCASH_LASTSUPPLIER":2,
"ALT_UM":"Kg",
"DISCOUNT":"0",
"LASTSUPPLY_QTY":"3",
"LASTSUPPLY_NIR":5,
"RECVERSION":284,
"NAME":"PAINE DE SECARA 300G",
"ITEM_CODES":[
{
"SALECODE":"12",
"ITEM":1,
"SALECODE_NAME":"PLU"
}
],
"SALECODE_NAME":"PLU",
"ALT_UM_RATIO":"0.3",
"IDEXTAPP_LASTSUPPLIER":"",
"CODWITHCRC":"12"
}
]
}
]
}
Try using $json['result'][0]['DATASET'][0]['SALECODE'] instead.
If you look at your response data closely, you'll see it's heavily nested. result is also an array, so you'll need to choose an item from it. In this case, there's only one. If there are others, you'll need to loop over $json['result'].
<?php
$url = 'http://everywhere.smartcash.ro/everywhere/rest/TSmartCashMethods/GetArticleInfo/4/1/1/1/';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json' ));
$result = curl_exec($cURL);
curl_close($cURL);
$json = json_decode($result, true);
echo "Price: " . $json['result'][0]['DATASET'][0]['PRICE'];
echo "<hr>";
echo "Sales code: " . $json['result'][0]['DATASET'][0]['SALECODE'];
?>
I'm implementing freshdesk API's for one of my project.
Using freshdesk PHP API I tried to create new user but i'm getting internal server error when I run the code in postman plugin and i'm using SLIM framework for my project.
Please check my below code and thanks in advance!
PHP:
<?php
$api_key = "MY_API_KEY";
$password = "xyz123M";
$mydomain = "MY_DOMAIN";
$custom_fields = array(
"department" => "Accounts"
);
$contact_data = json_encode(array(
"user_name" => "Sri Jitthu",
"email_id" => "srij#mydomain.com"
));
$url = "https://$mydomain.freshdesk.com/api/v2/contacts";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201) {
echo "Contact created successfully, the response is given below \n";
echo "Response Headers are \n";
echo $headers."\n";
echo "Response Body \n";
echo "$response \n";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers."\n";
echo "Response is ".$response;
}
}
curl_close($ch);
?>
I have used same freshdesk PHP API. I think the parameters that you have defined is not acceptable by fresh desk.We need to pass the parameters as they defined.
Instead of passing user_name you should pass name also instead of passing email_id you should pass email.
As well as when you're passing custom fields in your API means that should exists or predefined in your account then only it will work. for ex you're passing department is Accounts in your API which means the Accounts should be predefined in your freshdesk account.
Check the below code and try. Hope it will help you.
PHP:
<?php
$api_key = "YOUR_API_KEY";
$password = "xyz";
$yourdomain = "YOUR_DOMAIN";
$custom_fields = array(
"department" => "Accounts"
);
$contact_data = json_encode(array(
"name" => "Sri Jitthu",
"email" => "srij#mydomain.com"
));
$url = "https://$yourdomain.freshdesk.com/api/v2/contacts";
$ch = curl_init($url);
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $contact_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
$info = curl_getinfo($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($server_output, 0, $header_size);
$response = substr($server_output, $header_size);
if($info['http_code'] == 201) {
echo "Contact created successfully, the response is given below \n";
echo "Response Headers are \n";
echo $headers."\n";
echo "Response Body \n";
echo "$response \n";
}
else
{
if($info['http_code'] == 404)
{
echo "Error, Please check the end point \n";
}
else
{
echo "Error, HTTP Status Code : " . $info['http_code'] . "\n";
echo "Headers are ".$headers."\n";
echo "Response is ".$response;
}
}
curl_close($ch);
?>
Also check your API key and password one more time before sending the request whether both are correct or wrong?
I can't make it work.. :( I have this function (for create passthrough transcoder), when I run I see NULL in the web. If I test directly from the browser with the url, it does notify me that there is a problem an auth (apikey and acceskey)
function createPassthrough($name, $source_url, $recording = null)
{
$url = "https://sandbox.cloud.wowza.com/api/v1/transcoders";
$json = '{
"transcoder":{
"billing_mode":"pay_as_you_go",
"broadcast_location":"eu_belgium",
"delivery_method":"pull",
"name":"prueba",
"protocol":"rtsp",
"source_url":"url_camara",
"transcoder_Type":"passthrough",
"low_latency":true,
"buffer_size":0,
"play_maximum_connections":100,
"stream_smoother":false
}
}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json; charset=utf-8',
'Content-Type: application/json; charset=utf-8',
'wsc-api-key:' . $apiKey,
'wsc-access-key:' . $accessKey,
));
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
var_dump($obj);
}
What am I doing wrong? Thanks in advance.
You should check for curl errors after $result = curl_exec($ch);.
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
I am trying to pass a json string into a curlopt_postfield.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
I have a working hard coded example. However I am having trouble making it dynamic.
$test = "84560";
$data = "{\"id\":\"" .$test. "\"}";
$data2 = "{\"id\":\"84560\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $data2);
Why does $data2 work and $data fail?
I have tried json encoding, utf8 encoding, and lots of variations of escaping the quotes all to no avail. What am I missing? I do not get an error simply a NULL response.
Here is the full code. Still not able to get it working. Any additional suggestions as I have tried everything I have found.
$test = "84560";
$data = "{\"id\":\"" .$test. "\"}";
$data2 = "{\"id\":\"84560\"}";
$result2 = getJson("results.json?", $data2);
function getJson($endpoint, $postfields)
{
$URL = "https://app.mobilecause.com/api/v2/reports/". $endpoint;
$ch = curl_init();
$headers = [];
$headers[] = "Authorization: Token token=\"[token goes here]\"";
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if (curl_errno($ch))
{
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);;
return $result;
}
You should simply try:
<?php
$data = array('id' => 84560);
$json = json_encode($data);
echo $json; // Will print { id: 84560 }