I have a problem, I tried updating JSON formated data with CURL to my online database: Firebase. It works fine at first, but when I send Data witch I have sent somewhen before it doesn´t get stored. I am sending a combination of a few GET-Variables.
if($_GET['user']!='')
{
$user_url = str_replace('.' , ',' , $_GET['user']);
$url='https://websitenew.firebaseio.com/' . $user_url . '.json';
$data = array('author'=>$_GET['user'], 'temp'=>$_GET['temp'], 'druck'=>$_GET['press'], 'notMoving'=>$_GET['move'], 'gpsx'=>$_GET['gpsx'], 'gpsy'=>$_GET['gpsy']);
$data_json = json_encode($data);
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_json));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_exec($ch);
curl_close($ch);
}
Thanks!
This should Ideally work with get Variable. If that does not work then try mapping it to a variable in php and then creating a JSON out of it. If the API does not work. Please try out the below one which I had used in my project and it is fully functioning too.
https://github.com/eldhosemjoy/firebase
I hope this git could help you understand the operations with firebase too.
Related
I'm trying to access the OCCRP Aleph API
https://redocly.github.io/redoc/?url=https://aleph.occrp.org/api/openapi.json
with the following PHP program
$url = 'https://aleph.occrp.org/api/2/entities?q="title:bank"';
$ch = curl_init();
$headeroptions = array('Accept: application/json', 'Content-Type: application/json', 'Authorization: ApiKey 363af1e2b03b41c6b3adc604956e2f66');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headeroptions);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$array = json_decode($data, true);
Var_dump($array);
I always get the error "No schema is specified for the query."
When I call Statistics, I get a correct answer...
after inspecting how the API works in their own website, I've notice you're missing a required parameter, which indicates the schema. Just add this to your url:
&filter:schemata=Thing
and it should work
I am trying to use the Zoho API Version 2 to do a simple record update in Leads. I am using PHP and CURL and my sample code for this call (to update a single field in the record) is as follows:-
$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($fields),
sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);
$fields = json_encode([["data" => ["City" => "Egham"]]]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
curl_close($ch);
No matter how I format the JSON, I always get the following returned:
{"code":"INVALID_DATA","details": {"expected_data_type":"jsonobject"},"message":"body","status":"error"}
It is not a matter of invalid auth tokens etc because I have successfully used PHP and CURL with the Zoho API to read data and I decode the JSON returned successfully.
Please could somebody help with passing valid JSON data?
The above code constructs input JSON like this
[{"data":{"City":"Egham"}}]. This JSON is not valid as per the ZOHO CRM APIs(API help).
It should be like this {"data":[{"City":"Egham"}]}.
Change the code like this :
$apiUrl = "https://www.zohoapis.com/crm/v2/Leads/" . {valid record id here};
$fields = json_encode(array("data" => array(["City" => "Egham"])));
// *strlen must be called after defining the variable. So moved headers down to $fields*
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($fields),
sprintf('Authorization: Zoho-oauthtoken %s', {valid auth token here})
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
curl_close($ch);
You must escape the data into JSON format:
import json
response = requests.post(url, data=json.dumps(data), headers=headers)
Thank you in advance
I tried to get odata service url using curl using following code
<?php
$url = "serviceurl/odata.srv/Users?$format=json&$expand=EmployeeDetails/ClientDetails/ClientConfigurationDetails,EmployeeDetails/ClientDetails/LogoDetails,SalutationTexts,UserConfigurationDetails";
$ch = curl_init($url); // such as http://example.com/example.xml
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
only resulting the user details the expand details are not there but in postman and calling directly in browser its working fine getting all details
Give the url in single quotes like below
$url = 'serviceurl/odata.srv/Users?$format=json&$expand=EmployeeDetails/ClientDetails/ClientConfigurationDetails,EmployeeDetails/ClientDetails/LogoDetails,SalutationTexts,UserConfigurationDetails';
While using double quotes php will try to replace the $format and $expand variables which are actually odata parameters not php variables.
Hope this helps.
I am in the process of creating a super simple PHP page that sends JSON data to an remote API via POST. I am having issues with it and hoping someone here could help me out.
This is what I have so far
$url = "https://remoteHost.com/api/post";
$json = '{"message":"textHere", "user":"userABC"}';
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic --ACB123-ABC123---ABC123---',
'Content-Type: application/json',
'Content-Length: ' . strlen($json),)
);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
When I execute this nothing happens. The page loads without errors but it looks like the curl isn't actually doing anything. Any Ideas?
after I succeeded in making a POST request and grab the values in the web service i'm building. I'm facing a problem regarding the Put Request. I managed to make the Put Request and I sent an array containing name and id for the update purpose this way:
curl_setopt($ic, CURLOPT_POSTFIELDS, http_build_query($data));
But when I try to grab the id sent using $_POST['id'] I get the undefined index error, I printed_r($_POST) and it's empty. Now I dont't believe there is a super global array for PUT like for POST and even if it exists , I don't think there is :
curl_setopt($ic, CURLOPT_PUTFIELDS, http_build_query($data));
in curl have you even been through a similar error? any idea?
To take a look at my previous post concerning the post request to have a better understanding of what I'm trying to do , it's here
Try this
curl_setopt($ic, CURLOPT_PUTFIELDS, json_encode($data));
and take it by
$array_get = json_decode(file_get_contents('php://input'));
use this CURLOPT_CUSTOMREQUEST =PUT and then just set values with CURLOPT_POSTFIELDS
or
you can use custom header CURLOPT_HTTPHEADER
eg.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
The script below demonstrates how you make a PUT request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // note the PUT here
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
// execute the request
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
The $_POST only for method=post;
You use method=put, so the $_POST is empty.
You can get the putdata like this:
$_PUT = array();
if('PUT' == $_SERVER['REQUEST_METHOD']){
parse_str(file_get_contents('php://input'), $_PUT);
}
This is the secret to sending a PUT request:
curl_setopt($ch, CURLOPT_POST, true); // <-- NOTE this is POST
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // <-- NOTE this is PUT
A full example:
$vals = array("email" => "hi#example.com", "phone" => "12345");
$jsonData = json_encode($vals);
curl_setopt($ch, CURLOPT_POST, true); // <-- NOTE this
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // <-- NOTE this
//We want the result / output returned.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:' . strlen($jsonData),
'X-Apikey:Any_other_header_value_goes_here'
));
//Our fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
//Execute the request.
$response = curl_exec($ch);
echo $response;