PHP: Adding entries from php to json - php

I have no knowledge of php, but from an iOS app I am trying to pass variables to json which can be accessible later, each time user complete level the post method push 3 variables and its value which adds that info into file like this
php code:
<?php
header ('Location: ');
$handle = fopen("data.json", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, ":");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
and I get the output like this after two users used post method:
Name:user1
Points:100
Level:1
Name:user2
Points:200
Level:2
can someone please help me to get json format output, with every time user push data with the post method it adds info to existing data instead of overwriting it?
I want output like this:
[
{"Name":"user1","Score":"100","Level":"1"},{"Name":"user2","Score":"200","Level":"2"}
]

<?php
$json = json_decode(file_get_contents("data.json"), true);
$json[] = json_encode($_POST);
file_put_contents("data.json", $json);
This decodes the current JSON file, then appends the new information and then saves over the existing JSON file.

Related

Pipedrive Webhook PHP endpoint code example

I have setup a webhook from Pipedrive to activate on a deal being updated. I am struggling with the correct way to read the json response in php. I am new to Webhooks so is there another way of reading the response data. This is what I have:
<?php
$result = $_REQUEST['current']; // this is where it is not working I believe
$obj = json_decode($result, true);
$txt = $obj['id']
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
In requestbin I am getting a response with all the correct information I am expecting
It appears that body of the event contains an array called "current" with all the relevant data in and I wanted to extract the "id" to see it working
"body": {
"current": {
"id": 71}
Any help really appreciated
I have found an answer that shows me what I require
$result = file_get_contents("php://input");
This pulls back the full response from the server showing data

Json decode returns nothing

I am working with Webhook to receive data, so I can store it into database, so I got data as Json and tried to decode it, so I can retrieve each one using json_decode and save them in a text file but I got nothing the file was empty
so here is me code :
<?php
require_once('vendor/autoload.php');
$payload = #file_get_contents('php://input');
if (isset($payload) && !empty($payload)) {
$data = json_decode($payload, true);
$myFile2 = "file.txt";
$myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
$newnbord = $data;
fwrite($myFileLink2, $newnbord);
fclose($myFileLink2);
}
and this is an example of data I received through webhook :
{"accountingCurrency":"USD","address1":"test address","billedCurrency":"EUR","billedRecurringPrice":"0.00","bin":"444444","cardType":"VISA","city":"testcity","country":"FR","initialPeriod":"30","lastName":"test","timestamp":"2020-11-12 06:05:49","username":"llllll","threeDSecure":"NOT_APPLICABLE"}

Recieving Blank/No Response to Status Update Notification(statusUpdateNotification) from Apple Server - PHP v5.4.43

We have set up a URL in the app profile in iTunes and our server has cleared the ATS security criteria.
Following are the codes that we have tried to implement :
$data = json_decode(file_get_contents('php://input'), true);
$fp = fopen('appdata.txt', 'a');
fwrite($fp, $data);
fclose($fp);
We got no response with this code.
Then we tried -
$data = print_r($_REQUEST, TRUE);
$fp = fopen('appdata.txt', 'a');
fwrite($fp, $data);
fclose($fp);
We get a blank array in our 'appdata.txt' file as -
Array
(
)
Is there any way to find out if we are even receiving a response from Apple server?
Finally, we have solved the issue :)
While checking error log file we find this error message -
PHP Warning: "fwrite() expects parameter 2 to be string, array given."
So we replaced this line in our first code -
fwrite($fp, $data);
with this -
fwrite($fp, print_r($data, true));
Now the code is working fine and we are getting the Status Update Notification every time.

How to parse JSON into CSV in PHP for Social Mention API

I need to query SocialMention.com for "Colorado Springs" and "District 11" and pull the data back into a CSV file. I'm an old programmer but new to PHP and JSON. Here's the simple php code:
<?php
$curl = curl_init('http://api.socialmention.com/search?q=%22colorado+springs%22+%22district+11%22&f=json&src[]=twitter&sentiment=true&meta=info');
$result = curl_exec($curl);
echo $result;
?>
How do I parse $result and store it in a CSV file? I've been researching this for 6 days and have tried every example I can find with no luck. In the examples below I keep getting an "Invalid argument in foreach" error.
Your question is broad since you don't really say how you want to parse $result, but you're not even getting $result because curl_exec will actually print out its contents. Instead you can set curl_setopt($curl, CURLOPT_RETURNTRANSFER, true), or you can probably just use$result = file_get_contents($url);
Anyway, you can change the result from JSON to usable PHP data structure via $data = json_decode($result, true). The second argument forces associative arrays, which are probably better for your purposes.
Then you can write to the CSV file:
$fh = fopen('csvfile', 'w');
foreach ($result['items'] as $items) {
fputcsv($fh, $items);
}
u can refer the two links too
1.Parsing JSON file with PHP
2.php writing to csv from an array
$json_a=json_decode($string,true); //decodes json
//writes to csv
$fp = fopen('file.csv', 'w');
for ($i = 0; $i < count($json_a); $i++) {
$fields = //json object feids here
fputcsv($fp, $fields);
}
fclose($fp);

Using PHP to store results of a post request

Im currently working with an API which requires we send our collection details in xml to their server using a post request.
Nothing major there but its not working, so I want to output the sent xml to a txt file so I can look at actually whats being sent!!
Instead of posting to the API im posting to a document called target, but the xml its outputting its recording seems to be really wrong. Here is my target script, note that the posting script posts 3 items, so the file being written should have details of each post request one after the other.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// get the request data...
$payload = '';
$fp = fopen('php://input','r');
$output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
fwrite($output_file, $payload);
}
fclose($fp);
fclose($output_file);
?>
I also tried the following, but this just recorded the last post request so only 1 collection item was recorded in the txt file, instead of all 3
output_file = fopen('output.txt', 'w');
while (!feof($fp)) {
$payload .= fgets($fp);
}
fwrite($output_file, $payload);
fclose($fp);
fclose($output_file);
I know im missing something really obvious, but ive been looking at this all morning!
Change
$output_file = fopen('output.txt', 'w');
to
$output_file = fopen('output.txt', 'a');
Also, change
$payload .= fgets($fp);
to
$payload = fgets($fp);
you should probably use curl instead to fetch the content and then write it via fwrite
change
$payload .= fgets($fp);
to
$payload = fgets($fp);

Categories