Load Json data from file into Parse.com backend via Rest API - php

Evening folks,
I am attempting to load data from a .json file into parse.com backend. I can get the rest api to load manually added json as per the example provided from Parse.com from the terminal on my Mac:
curl -X POST \
-H "X-Parse-Application-Id: removed" \
-H "X-Parse-REST-API-Key: removed" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/Test
This works fine.
How can I read a saved .Json file with only these three parameters(Json is validated fine) and send the data to my Parse backend?
"score":1337,"playerName":"Sean Plott","cheatMode":false
I believe I have the option to use PHP/Jquery etc. however I have not used any of these languages extensively and a quick code example would be very much appreciated.
Thanks,
Gerard

Here is pure PHP solution:
$url = 'https://api.parse.com/1/classes/Test';
$fields_string = file_get_contents('file.json');
//open connection
$ch = curl_init();
//set the url, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
/* End of first request */

Related

php curl post missing parameters

I have a curl request to get some information about database and stuff, but since some user need to query many bases and most of them are not familiar with any kind of shell,
we are trying to build a small page to e"xecute the query and be more user friendly
we got pretty much everything working (form and stuff) but we got really figure how to build the uery in php
here is the original working curl request we made:
curl -s - X POST -H 'Content-type:application/json' -H
'Accept:application/json' -d '{"param1":"value1","param2":"value2"} -k
https:myurl -u user:password
and here is the code with the request we are trying to build:
<?php
$data = array('param1' => 'value1',
'param2' => 'value2'
);
$ch = curl_init();
$url = "https://myurl";
curl_setopt($ch, curlopt_url, $url);
curl_setopt($ch,curlopt_userpwd, "user:password" );
curl_setopt($ch,curlopt_post, 1);
curl_setopt($ch,curlopt_postfuelds, $data);
curl_setopt($ch,curlopt_returntransfer, true);
curl_setopt($ch,curlopt_ssl_verifypeer, false);
curl_setopt($ch,curlopt_httpheader,
array ("Content-type:application/json"));
$output = curl_exec($ch);
curl_close($ch);
I cannot really figure whats wrong? can you help me?
I rather not use shell_exec with the curl inside because the server hosting is on windows, and we don't have curl.exe available
thanks
You have a typo: curlopt_postfuelds should be curlopt_postfields

Using PHP to create Confluence Wiki pages

I've been able to use PHP in order to generate JIRA issues, but was wondering if it is possible to also create Confluence wiki pages that link back to the JIRA issue. I haven't been able to make this work yet. Does anyone have any examples on how this is accomplished?
With this code in php, you can create Confluence Pages:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"type\":\"page\",\"title\":\"inserttitle\",\"space\":{\"key\":\"insertspace\"},\"ancestors\":[{\"type\":\"page\",\"id\":insertancestor}],\"body\":{\"storage\":{\"value\":\"<p>This is a new page</p>\",\"representation\":\"storage\"}}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "insertusername" . ":" . "insertpassword");
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
You can easily create a new Confluence page using the REST API. Here's an example using curl:
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/
The next thing to do would be for PHP to call that. Check the following for examples:
PHP curl post
Basic authentication in PHP curl
Having done that, you could also embed a JIRA macro in the Confluence page for extra zing. That would mean that the original curl would add the JIRA macro as the storage format (in a Confluence page, click on Tools (or "..." now) and select View Storage Format for an example.
Here's an example of the JIRA macro:
<ac:structured-macro ac:name="jira">
<ac:parameter ac:name="server">Example JIRA</ac:parameter>
<ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter>
<ac:parameter ac:name="key">XYZ057-172</ac:parameter>
</ac:structured-macro>
You'll need to figure out your server name, serverId & JIRA issue key yourself.
This would mean the original curl looks like this:
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page with a JIRA macro added:</p><ac:structured-macro ac:name="jira"><ac:parameter ac:name="server">Example JIRA</ac:parameter><ac:parameter ac:name="serverId">fdsafds-68es-3615-a6f7-71427b983092</ac:parameter><ac:parameter ac:name="key">XYZ057-172</ac:parameter></ac:structured-macro>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/

Cannot add attachment to Zephyr test using ZAPI API

I have spent countless hours trying to get the Attachment Resource API(s) to work with no avail. I have referred to the docs here: http://docs.getzephyr.apiary.io/#executionresourceapis
But they are not much help and Zephyr support has not responded to any of my questions in the last 3 months.
Here is my curl call:
curl -D- -u user:pass -X POST -H "Content-Type: multipart/form-data" -H "X- Atlassian-Token: nocheck" -F "file=/home/jared/apiautomation/output.html" "https://jiraurl/rest/zapi/latest/attachment?entityId=3019&entityType=execution"
I have also tried php:
<?php
$url = "http://jiraurl/rest/zapi/latest/attachment?entityId=3091&entityType=execution";
$upass="";
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $upass);
$file_name_with_full_path = realpath("/home/jared/postman/authentication/output.html");
$post = array("file=#.$file_name_with_full_path; filename=output.html;");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Atlassian-Token: nocheck'));
$response = curl_exec($curl);
curl_close ($curl);
?>
for both examples i get unsupported media type. which doesnt make sense because I can attach it through Jira. Im completely lost at this point. Ive referenced:
https://answers.atlassian.com/questions/268253/add-attachment-to-test-execution-using-zapi
Please help. :)
This was answered on Atlassian Answers but here is a copy for this question here.
1) entityType
This is the item you will be attaching to. Currently the only types are 'Execution' and 'TestStepResult'
2) entityId
This is the actual Id of the item whose type you chose. If it is an Execution type, you will need a 'scheduleId'. If it is a TestStepResult, you will need a 'testStepId'
The CURL for adding an attachment via ZAPI is formatted as:
curl -D- -u : -X POST -H "X-Atlassian-Token: nocheck" -F "file=#name_map.jpg" "http://192.168.100.144:9122/rest/zapi/latest/attachment?entityId=&entityType="
Note: "X-Atlassian-Token: nocheck" is required
For sample code in Python of attaching to a entity of type 'Execution', please see our Community Forum post here:
http://community.yourzephyr.com/viewtopic.php?f=21&t=1382
Regards

Convert terminal cURL command to PHP cURL request

I am trying to send a cURL request from PHP to Here Maps' RESTFUL Batch API.
The documentation states, that using this cURL script, I can send a data file with the data I need to them:
curl -X POST -H "Content-Type: text/plain" --data-binary #addresses.txt
"http://batch.geocoder.cit.api.here.com/6.2/jobs?
&app_code=AJKnXv84fjrb0KIHawS0Tg
&app_id=DemoAppId01082013GAL
&action=run
&header=true
&inDelim=;
&outDelim=,
&outCols=recId,latitude,longitude,locationLabel
&mailto=<my_email>
&outputcombined=true
&language=de-DE"
I am trying to convert this to PHP. I came up with the following code:
$cURLHandler = curl_init();
$url = "http://batch.geocoder.cit.api.here.com/6.2/jobs?&app_code=AJKnXv84fjrb0KIHawS0Tg&app_id=DemoAppId01082013GAL&action=run&mailto=trisztanthar#mailinator.com&outCols=recId,latitude,longitude,locationLabel&outputcombined=true&inDelim=;&outDelim=;";
if($cURLHandler) {
curl_setopt($cURLHandler, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($cURLHandler, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($cURLHandler, CURLOPT_POST, 1);
curl_setopt($cURLHandler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, array('addresses' => new CurlFile("./batchjob.txt", 'text/plain', "batchjob.txt")));
curl_setopt($cURLHandler, CURLOPT_URL, $url);
curl_exec($cURLHandler);
curl_close($cURLHandler);
}
else {
throw new RuntimeException("Nem sikerült felvenni a kapcsolatot egy távoli szerverrel.");
}
When using it, I get the following error:
Only pipe (|), semicolon (;), colon (:), tab (' ') and comma (,)
delimiters allowed.
First I thought, that there was a problem with the file, however I tried to run the terminal command in a unix terminal with the same file I tried to use with PHP, and it went through without any problems, responded with a proper response, so its 100% that the PHP code I'm using isn't posting the file.
I'm currently developing on localhost, with MAMP Server (OSX). The batchFile.txt file is in the /Applications/Mamp/htdocs/php/ folder (localhost/php/batchfile.txt).
What am I doing wrong?
From your commandline you are directly posting the file as binary data. So send the content like below.
curl_setopt($cURLHandler, CURLOPT_POSTFIELDS, file_get_contents("batchjob.txt"));

Is it possible to use cURL to stream upload a file using POST?

Or does PHP not allow this? I have read it is possible using PUT but the server is expecting POST only.
cURL has already support for streams, try curl --help | grep binary and you will get:
--data-binary DATA HTTP POST binary data (H)
An example:
curl -v -XPOST http://example:port/path --data-binary #file.tar
-H "Content-Type: application/octet-stream"
Yes it is possible to stream upload a file using POST file uploads with cURL. This is the default and you need to provide the filename of the file(s) you would like to stream in form of strings.
// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "#c:/logs.log";
// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
// Just for debug: to see response
echo $response;
Apart from that default method, it is not possible to use cURL to stream upload a file using the POST method.
Command to upload a binary file
curl --location --request POST 'http://localhost:8080/hello/read' \
-H "Content-Type: application/octet-stream" \
--data-binary '#/Users/krishna/Documents/demo.bin'
command to upload a text file
curl --location --request POST 'http://localhost:8080/hello/read' \
--header 'Content-Type: text/plain' \
--data-binary '#/Users/krishna/Documents/demo.txt'
Set the Content-Type as per the file content.

Categories