I am trying to receive JSON data from an external system, and then process and save to my local DB. When an event triggers, the external system sends the data below to my system.
The external system logs show:
Log 1: Date/Time: 16 August 2017 11:54:44 AM
Status: Failure
Server: Apache https://blahblah.com/abc/data.php
Status Code: OK
Event Content: [{"field":"_USERNAME","value":""},
{"field":"_PASSWORD","value":""},
{"field":"_TOKEN","value":""},
{"field":"_CODE","value":"L77H4XD6ZA"},
{"field":"_SUBMITTEDDATE","value":"2017.08.16.01.54"},
{"field":"_SUBMITTEDDATEEXT","value":"2017.08.16.01.54.39.610"},
{"field":"_EDITEDDATE","value":"2017.08.16.01.55"},
{"field":"_SEQUENTIALID","value":"39a1cad9-2582-e711-9477-06c7814985cc"},
{"field":"_COMPLETETYPE","value":"Complete"},
{"field":"_LANGUAGE","value":"en"},
{"field":"_TOTALTIME","value":"12.59"},{"field":"_LINKURL","value":"http%3a%2f%2fsurv.blah.com%2ftest3%3fusr%3dL77H4XD6ZA"},
{"field":"GENDER","value":"TEXT%3aFemale%3bVALUE%3a2"},
{"field":"AGE","value":"TEXT%3a40%2b-%2b44%3bVALUE%3a6"},
{"field":"STATE","value":"TEXT%3aVIC%3bVALUE%3a2"},
{"field":"END_CHC","value":"TEXT%3aComplete%3bVALUE%3a2"},
{"field":"D2H","value":""},
{"field":"D2V","value":""},
{"field":"PCODE","value":""},
{"field":"PSTATE","value":""},
{"field":"PREGION","value":""},
{"field":"STATEREGION","value":""},
{"field":"TEST1","value":""}]
So, in my php file (https://blahblah.com/abc/data.php) I have
$json = file_get_contents('php://input');
$obj = json_decode($json);
and I have been trying a foreach() loop to try and get each field and value, to then put into a database, like
foreach($obj as $key => $value)
{
$qry = $conn->prepare('INSERT INTO `key_value`(`db_id`, `rkey`, `rvalue`) VALUES (NULL,$key,$value)');
$qry->execute();
}
But it isn't working, and I either get errors about the foreach loop, or if I play around with just echoing or var_dumping, I get NULL in the response, when I use the Postman or ARC Chrome API testing Apps.
So, I suspect I am on the completely wrong track of how to do this.
Can anyone help guide me back?
EDIT: I have had a look at Receive JSON POST with PHP and a few others, but the working answers aren't clear.
You've got a couple of problems here. First, you need to pass true as the second parameter to json_decode in order to get an associative array instead of a stdClass. Then, when you're iterating the results, you don't need the $key, and the $value is each entry in the results array (each "row"). Try running this to see what's going on:
<?php
$json = <<<JSON
[{"field":"_USERNAME","value":""},
{"field":"_PASSWORD","value":""},
{"field":"_TOKEN","value":""},
{"field":"_CODE","value":"L77H4XD6ZA"},
{"field":"_SUBMITTEDDATE","value":"2017.08.16.01.54"},
{"field":"_SUBMITTEDDATEEXT","value":"2017.08.16.01.54.39.610"},
{"field":"_EDITEDDATE","value":"2017.08.16.01.55"},
{"field":"_SEQUENTIALID","value":"39a1cad9-2582-e711-9477-06c7814985cc"},
{"field":"_COMPLETETYPE","value":"Complete"},
{"field":"_LANGUAGE","value":"en"},
{"field":"_TOTALTIME","value":"12.59"},{"field":"_LINKURL","value":"http%3a%2f%2fsurv.blah.com%2ftest3%3fusr%3dL77H4XD6ZA"},
{"field":"GENDER","value":"TEXT%3aFemale%3bVALUE%3a2"},
{"field":"AGE","value":"TEXT%3a40%2b-%2b44%3bVALUE%3a6"},
{"field":"STATE","value":"TEXT%3aVIC%3bVALUE%3a2"},
{"field":"END_CHC","value":"TEXT%3aComplete%3bVALUE%3a2"},
{"field":"D2H","value":""},
{"field":"D2V","value":""},
{"field":"PCODE","value":""},
{"field":"PSTATE","value":""},
{"field":"PREGION","value":""},
{"field":"STATEREGION","value":""},
{"field":"TEST1","value":""}]
JSON;
$obj = json_decode($json, true);
foreach($obj as $currTuple)
{
echo $currTuple['field'].':'.urldecode($currTuple['value'])."\n";
}
Also some of the results are url encoded, so you'll probably want to decode that before persisting.
I'm using the Bit2Check.com API. I want to check emails registered on PayPal. But after supplying email address to the API, I get this response:
{"Paypal":"Linked"}
I just want to grab the "Linked" part.
You are providing me with no code, so I will have to try to answer your question in general.
You probably have
{"Paypal" : "Linked"}
stored within some variable. To access the "Linked" part, you need to json_decode() it. Code retrieving just the "Linked" string would like something like this.
$yourVariable = '{"Paypal" : "Linked"}';
$decoded = json_decode($yourVariable);
// Access only linked part
$onlyLinked = $decoded->Paypal;
// Or maybe you are interested in boolean value?
$isLinked = ($decoded->Paypal == "Linked") ? true : false;
To read more about json_decode(), visit this article on php.net.
I'm using PHP to build an application that gets some statistics from individual Instagram accounts.
I do a get request to the following URL to get a JSON formatted string:
https://www.instagram.com/{username}/?__a=1
With json_decode, I get the followers and the followings by:
$follows = $data['user']['follows']['count'];
$followedBy = $data['user']['followed_by']['count'];
But I don't know how to get the posts count.
For example for NASA profile:
https://www.instagram.com/nasa/
... I need the number (currently) 1.967.
Any idea?
And a final question: do I have to use API Key to get the stats, or the GET to the above URL is enough?
what is wrong with
$json = file_get_contents('https://www.instagram.com/nasa/?__a=1');
$obj = json_decode($json);
echo $obj->user->media->count;
it just spit out the correct post count as you wanted?
As of now (2020) Instagram has made some changes to the JSON response and the code below does the same as the accepted answer above, also with formatted output.
$ig = json_decode(file_get_contents('https://www.instagram.com/nasa/?__a=1'));
echo number_format($ig->graphql->user->edge_owner_to_timeline_media->count);
I am facing some logical issue. I am fetching data via upi status table which look like as follows.
on controller file i am creating the following logic..
$this->load->model('localisation/upc_status');
$statusArray = $this->model_localisation_upc_status->getUPCStatuses();
$this->data['statuses'] = $statusArray;
foreach ($statusArray as $sa){
$c[] = array($sa['status_id'] => $sa['name']);
}
echo $c['8'];
i am unable to get the name in-stock. instead i am getting Array in-place of it.
Try This,
$c[$sa['status_id']] = $sa['name'];
If you still can't get, adjust levels of the array.
Use print_r(array) to check arrays at required debugging points.
I'm trying to get a user's site_id from posterous. This data seems to be nowhere in the user's profile, and nothing in the FAQ or help or docs indicates how to find this.
The only method I can find is to request a list of sites over API,
So, that's what I'm trying to do, and I get back a json response, and, indeed, the site_id of any site the user has on posterous is in there, and I just want to output this.
First I tried with (where $result is the json response)
$siteid = preg_match('"site_id": \d+', $result);
echo $siteid;
I get nothing.
Then I tried with
json_decode($result);
echo $result->site_id;
I still get nothing.
I can see, in the json response ($result),
"site_id": $somenumber;
Why can't I extract this data with either preg_match or json_decode? Is there another, better method?
My full code is pasted at http://tonybaldwin.me/paste/index.php?3u
The Posterous list-of-sites API returns a list (as you might expect) of mappings. Thus, what you need to do is first pick the right item of the list, and then read it's id key.
$result = json_decode($response);
$first_result = $result[0];
$first_result_id = $first_result["id"];
echo $first_result_id;