I'm trying to figure out how to send this kind of data, via PHP, to a RESTful service:
{
"api_version": 1.0,
"project_id" : 123,
"batch_id" : 111,
"accesskey" : "abc123",
"job": {
"file": file,
"word_count": 111,
"title": "FAQ",
"cms_id": "page_123_en_sp",
"url" : "http://original_post_url.com",
"is_update": false,
"translator_id": 123,
"note": "Translate these words",
"source_language": "en",
"target_language": "it"
}
}
First of all, I cannot use curl, as who's going to use this code might not have it installed or might be not allowed to install it.
That's not a 'normal' JSON object.
The job->file property is an actual file (not even an url to a file).
That's the actual code I'm using to send all requests and it worked, until I've met this specific case: http://pastebin.com/6pEjhAkg
The 'file' property is created as such:
$file = generate_file( $content );
protected static function generate_file($content) {
$file = fopen('php://temp','r+');
fwrite($file, $content);
rewind($file);
return $file;
}
Now, when sending data, with $params argument properly set on PHP side, the RESTful returns an error about missing 'api_version' and 'project_id', but they are present.
I'm pretty sure the problem is that he's not receiving data as JSON, but how can I convert to JSON an object that, in his properties, contains a file pointer resource?
The code that sends data and builds the file has been created by a former developer, and I can't get in touch with him.
I tried to understand what is wrong there and the only thing I managed to fix so far, for another unrelated issue, is to actually send JSON data (when $multipart==false), as you can see in lines 16-19 of the linked code, rather than sending urlencoded data.
Any hint?
JSON does not have a "file" concept. You can either load the file content, encode it using Base64 and then stuff it into a JSON string - or you can use multipart format for sending the file in one part and the complete JSON in another part. The multipart solution should be the best performing as it doesn't have to base64 encode the file content.
Here is a similar example message format from Mason that tries to formalize the json/multipart usage:
POST /projects/2/issues HTTP/1.1
Accept: application/vnd.mason+json
Content-Type: multipart/form-data; boundary=d636dfda-b79f-4f29-aaf6-4b6687baebeb
--d636dfda-b79f-4f29-aaf6-4b6687baebeb
Content-Disposition: form-data; name="attachment"; filename="hogweed.jpg"
... binary data for attached image ...
--d636dfda-b79f-4f29-aaf6-4b6687baebeb
Content-Disposition: form-data; name="args"; filename="args"
Content-Type: application/json
{
"Title": "Hogweeds on the plaza",
"Description": "Could you please remove the hogweeds growing at the plaza?",
"Severity": 5,
"Attachment":
{
"Title": "Hogweed",
"Description": "Photo of the hogweeds."
}
}
Related
I'm using currently the Firebase messaging with PHP. I was able to make it work with a single notification with PHP and cURL. I've read the documentation about making batch request and I've constructed the request string as follow:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.xxxxxnY
POST /v1/projects/xxxxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"cxxxxx3",
"data":{
"typeNoti":"paiement",
"idcompte":"admin",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer yaxxxxxxY
POST /v1/projects/xxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"eoxxxxxU1",
"data":{
"typeNoti":"paiement",
"idcompte":"compte2",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary--
This string is generated in a file called batch_request.txt as the documentation said. When trying to send the request with cURL I receive this error:
{
"error": {
"code": 400,
"message": "Failed to parse batch request, error: 0 items. Received batch body: (0) bytes redacted",
"status": "INVALID_ARGUMENT"
}
}
I've experienced a strange behavior also, which is when I construct that string manually (in PHP I use HEREDOC), the batch request does occurs with 200ok response. I've tried to find some hidden character but couldn't.
I know there is a library called firebase-php. The problem with it is that I cannot find a straight foreward example like this:
//authenticate with json file
//create new token if it doesn't exist or skip
//create notification array
//create data array
//send multi notifications to multiple devices
I actually use a for loop and mono notification (one device token, one message at a time). The problem with this is that not all notifications are sent.
I appreciate your help thanks!
I gathered the code from firebase-php docs. After installing the library with composer I get a lot of errors and a lot of missing files in Firebase directory inside vendor like Factory.php etc
My solution was to NOT USE firebase-php library due to the numerous bugs I've faced (or maybe misuses) neither to construct the REST request text file for batch request. I used instead the NodeJS SDK which worked like a charm. I'm running a javascript file from my php code using exec.
I have some data that compressed with gzip in an application from here:
app.myaddress.com/data/api/1.
The data contains several parameters in JSON format like follows:
{
"id": 1,
"data": "abcabcabcabcabc" //this is the compressed data
}
I need to check the compressed data with another 3rd party service, we can just say the address like follows: app2.myaddress.com/check_data/abcabc by API request, but it's needed a header authentication:
{
"content-type": "application/json",
"api-key": 123456
}
app2.myaddress.com will return data JSON format like follows:
{
"name": "hello",
"address": "australia"
}
What I need to do is just checking a data by accessing URL like:
app.myaddress.com/data/api/checked/1
then the controller will process the data include checking through app2.myaddress.com and return the value into app.myaddress.com
Solved by #rafitio:
You can use cURL or Guzzle to access both URL inside your function. Here is the documentation of Guzzle: http://docs.guzzlephp.org/en/stable/request-options.html
I have to make a HTTP call to send data compressed data. I'm developing in Symfony2. For HTTP calls I'm using Guzzle client (version 3.8.1). Also, I am using Guzzle Service Descriptions to decribe the operations allowed on each command.
I know that I have to add the header "Content-Encoding: gzip" in the request, but the request body is not compressed.
Is there a way to specify in the Guzzle client that the request needs to be compressed? (maybe specify this in the Service Description)
Thank you!
To tell the server to give you compressed version, you have to inform it that you understand how to decompress the data.
For that purpose, you send a header during request which is called Accept-Encoding.
Example of accept-encoding header and values (those are the compression schemes that your client knows to use):
accept-encoding:gzip, deflate, sdch, br
The RESPONSE header Content-Encoding is sent by the server. IF that header is set, then your client asserts that the content is compressed and uses the algorithm that the server sent as value of Content-Encoding.
The server doesn't have to respond with compressed page.
Therefore, these are the steps:
Tell the server you know how to deal with compressed pages. You send accept-encoding header and then you specify which compression algorithms your client knows how to deal with.
Inspect whether the server sent Content-Encoding header. If not, content isn't compressed
If yes, check the value of the header. That tells you which algorithm was used for compression, it doesn't have to be gzip, but usually is.
The server doesn't have to respond with compressed page. You are merely informing the server you understand how to deal with compressed pages.
So, for you, what you should do is verify that your server sends gzipped responses and then you should set the request header accept-encoding. You got it the wrong way around.
I found a solution to send data compressed using Guzzle client with Operation Command and Service Description.
In the JSON file containing the service description I have specified that data sent in body is a string:
{
...
"operations": {
"sendCompressedData": {
"httpMethod": "POST",
"uri": ...,
"parameters": {
"Content-Type": {
"location": "header",
"required": true,
"type": "string",
"default": "application/json"
},
"Content-Encoding": {
"location": "header",
"required": true,
"type": "string",
"default": "gzip"
},
"data": {
"location": "body",
"required": true,
"type": "string"
}
}
}
}
}
As mentioned by #Mjh, Guzzle doesn't compress data automatically if "Content-Encoding" header is set, so data needs to be compressed before sending it to the Guzzle client to execute the command. I have serialized the object and used "gzencode($string)" for compressions.
$serializedData = SerializerBuilder::create()->build()->serialize($request, 'json');
$compressedData = gzencode($serializedData);
...
$command = $this->client->getCommand('sendCompressedData', array('data' => $compressedData));
$result = $command->execute();
I met an issue that the JSON string output to HTTP client was truncated near to close tag.
An example,
a. expect to see - ... ... "last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":null}
b. actually it is - ... ..."last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":nul
The last two characaters were truncated for some reason!!!
I tried both Postman on Chrome and curl on a console, all the same output. So looks not a browser specific issue. The JSON string in my application comes from a PHP json_encode on an associative array. The PHP code is using CodeIgniter framework running on Apache. I tried to write the json string into a file before http output, the file content is 100% correct. So it is not a json encoding issue in PHP.
The PHP is pretty straightforward. I have build below array (namely $finaldata) from database query
{
"success": true,
"data": [
{
"file_id": "1",
"title": "xxx",
"create_date": "2014-05-18 21:30:19",
"auditor": "1",
"status": "1",
"last_updater": null,
"last_update": "2014-06-10 20:43:14",
"garden_id": "1"
},
{
"file_id": "2",
"title": "yyy",
"create_date": "2014-05-18 21:30:19",
"auditor": "1",
"status": "1",
"last_updater": null,
"last_update": "2014-06-10 20:43:14",
"garden_id": "1"
}
],
"message": null
}
the "data" has a sub array and it could be a long array depends on database records. Then the variable $finaldata is passed to a output function that has below general logic:
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
$output = $this->format->factory($finaldata)->{'to_'.$this->response->format}();
header('Content-Length: ' . strlen($output));
The output function is built by a RESTful library from https://github.com/philsturgeon/codeigniter-restserver. In this context, it equals to
header('Content-Type: Application/json');
$output = json_encode($finaldata);
But I found an interesting thing that the issue only happened if the string length exceeds 8K. And when I append a random string like "ZZZ" to the JSON string before HTTP response, the issue was gone. I don't know the reason behind. It is not a correct hack because I could not prove 8K is a real threshold though.
Has anyone met such issue before? Any suggestion or comments are appreciated.
I think you send the data in UTF-8 charset so try to change this lines
header('Content-Type: '.$this->_supported_formats[$this->response->format]);
header('Content-Length: ' . strlen($output));
into this one
header('Content-Type: '.$this->_supported_formats[$this->response->format] . '; charset=utf-8');
header('Content-Length: ' . mb_strlen($output));
If this doesn't work just don't send the Content-Length header at all. This header is "only useful" if you send a file to the browser (as a download).
In my issue, I enable gzip in nginx,and append application/json to the field of gzip_types. Then, I set a header when make a request with Guzzle. It looks like
if (! isset($parameters['headers'])) {
$parameters['headers']['Accept-Encoding'] = 'gzip';
}
I'm trying to send bulk requests to the Piwik tracking api (/piwik.php) and I'm running into a problem. When I send the request (from a PHP script over ajax, curl and from fiddler2), I'm receiving the following:
Debug enabled - Input parameters:<br/>array ( )
token_auth is authenticated!
Loading plugins: { Provider,Goals,UserCountry }
Current datetime: 2013-05-02 16:02:27
The request is invalid: empty request, or maybe tracking is disabled in the config.ini.php via record_statistics=0
My post looks like this:
{"requests":["%3Fidsite%3D1%26url%3Dhttp%3A%2F%2Fexample.org%26action_name%3DTest+bulk+log+Pageview%26rec%3D1"],"token_auth":"mytokenhere"}
Which is the example straight from their website. I've made sure to set the content-type to "Content-Type: application/json" and that my configuration has record_statistics = 1 explicitly defined.
According to the documentation, this should all work, but I'm still getting the empty request. The import_logs.py script also works, so I know that the general bulk importing is not broken, but I'm not sure how to get the program to accept my data. Has anyone had any luck with it?
Thanks!
Perhaps the problem with your request is that your query strings are URL encoded, but they don't need to be since they're part of the POST body.
Your POST should be like this instead:
{"requests":["?idsite=1&url=http://example.org&action_name=Test+bulk+log+Pageview&rec=1"],"token_auth":"mytokenhere"}
See the example at the docs for the Bulk Tracking API: http://piwik.org/docs/tracking-api/reference/#toc-advanced-bulk-tracking-requests
Figured out what was wrong. Their documentation was incorrect in how the request needed to be formatted. First, URL Encoded data was unnecessary. Second, the JSON string needs to look like this:
{
"requests": [
{
"apiv": "1",
"bots": "1",
"idsite": "1",
"download": "",
"cdt": "",
"dp": "",
"url": "",
"urlref": "",
"cip": "",
"ua": "",
"_cvar": {
"1": [
"Not-Bot",
"Mozilla/5.0+(Macintosh;+U;+Intel+Mac+OS+X+10_6_5;+en-US)+AppleWebKit/534.10+(KHTML,+like+Gecko)+Chrome/8.0.552.231+Safari/534.10"
]
},
"rec": "1"
}
]
}
Not all of those pieces of data need to be sent, but that's the format necessary. After that it's just data cleansing.