0

I am running an app from AndroidHive which has feed.json which the app loads the content below from.

http://api.androidhive.info/feed/feed.json and this is the post url http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/

{
    "feed": [{
                "id": 1,
                "name": "National Geographic Channel",
                "image": "A URL",
                "status": "Science and etc"
                And Cosmos is all about making science an experience.
                ","
                profilePic ": "
                A URL ","
                timeStamp ": "
                1403375851930 ","
                url ": null
    }]
}

the above is the default content am wondering how to i add more to this content from or using a php script. I would love to know how to do it because I noticed that most application source code created in www_androidhive_info uses .json to load content which is an alternative to webview

Daniel
  • 10,641
  • 12
  • 47
  • 85
SIR-MYK3
  • 13
  • 6

3 Answers3

0

You can use json_decode() function to convert JSON string to object, add your variable, and then conver the object back to JSON

$obj = json_decode($json, true);
$obj->feed[0]->url = "google.com"
:
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
0
  1. Load the JSON
  2. Decode the JSON
  3. Add your content
  4. Encode to JSON
  5. Send it to your app

Code:

<?php

function failure($reason) {
    header('HTTP/1.1 500 Internal Server Error');
    echo json_encode(['failure' => $reason]);
    exit;
}

// 1. Load the JSON
$content = file_get_contents('http://api.androidhive.info/feed/feed.json');
if (!$content) {
    return failure('Failed to load upstream JSON');
}

//  2. Decode the JSON
$decodedContent = json_decode($content, true);
if ($decodedContent === false) {
    return failure('Failed to parse upstream JSON');
} elseif (empty($decodedContent)) {
    return failure('Upstream JSON empty');
}

// 3. Add your content
$decodedContent['feed'][] = [
    "id": 12,
    "name": "An example name",
    "image": "https://example.com/test_image.jpg",
    "status": "Hello!",
    "profilePic": "https://example.com/test_profile_pic.jpg",
    "timeStamp" => time(),
    "url" => "https://example.com"
];

// 4. Encode to JSON
$responseContent = json_encode($decodedContent);

// 5. Send it to your app
echo $responseContent;
jedifans
  • 2,287
  • 1
  • 13
  • 9
  • this might be a stupid question but am asking cause am confuse. am i going to save this above code to .php file if yes have already copied it and saved it as test.php but am running it offline in the sense am using wamps. i got error like this when i test run the test.php Parse error: parse error, expecting `')'' in C:\wamp\www\feed\test.php on line 5 which line 5 is echo json_encode(['failure' => $reason]); i really want my adding and editing to be as if am inputting figures as if am registering or posting when i click submit it updates the .json file (automatic) – SIR-MYK3 Aug 30 '16 at 08:07
  • Double check your php version. Only 5.6+ is receiving security updates. As to your additional question, you need a whole lot code to have a management system and keep the feed up to date with the third party. – jedifans Aug 30 '16 at 08:19
  • my wamp php version is 5.3.0 how can i get the management system script or making one ? http://stackoverflow.com/questions/22582471/is-there-a-json-api-based-cms-that-is-hosted-locally – SIR-MYK3 Aug 30 '16 at 14:33
0

Decode the JSON and make new function for changes then encode back.

<?php
$JsonVar = file_get_contents('http://api.androidhive.info/feed/feed.json');
$myjson = json_decode($JsonVar,true);

function Changes() {
    $myjson->feed[2]->name = "FeedID2";
    $myjson->feed[2]->id = "2";
    $myjson->feed[2]->image = "link to image.png";
    $myjson->feed[2]->status = "I am here.";
    $myjson->feed[2]->profilePic = "link to image.png";
    $myjson->feed[2]->timeStamp = time();
    $myjson->feed[2]->url = 'http://url.com';
}

json_encode(Changes());
?>
protld
  • 418
  • 1
  • 5
  • 12
  • how can i compile it to be an effective automatic .php script which will be like as if am posting new contents when i hit submit it will update the .json if its posible – SIR-MYK3 Aug 30 '16 at 08:19
  • @SIR-MYK3 - Check [this answer.](http://stackoverflow.com/a/39213067/6486232) – protld Aug 30 '16 at 09:39