I normally get all JSON data using Jquery however I currently need to do it using only PHP.
Essentially this is how I normally get and output it with Jquery: (Snippet, not whole code)
$.post('getdata.php', {uid:uid} , function(data){
if(data){
$.each(data, function(key, data) {
$('#div #span-'+key).html(data);
});
}
} , 'json')
Where uid is the value passed to php for use in querying the database.
And this is the php back end: (Again just a snippet of the JSON section)
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$ret["ID"] = $row['id'];
$ret["username"] = $row['username'];
$ret["email"] = $row['email'];
$ret["age"] = $row['age'];
echo json_encode($ret);
}
And obviously on the output page along with jquery I initiate the class and function:
$class = new retrieveData();
$class->userDetails();
Then to output the individual parts of the array where I want I would create a span with the same unique key for the particular array part as set out in jquery.
How would I go about converting the jquery part of this into PHP?
So if im understanding you you just want to rework the whole ajax side to sending off a request from the server:
$ch = curl_init($theUrlThatReturnsTheData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('uid' => $uid));
curl_setopt($ch, CURLOPT_POST, true);
$jsonData = curl_exec($ch);
$data = json_decode($jsonData);
Now do whatever it is you need to do with json... assuming from your jquery fragment thats
something like this:
<div id="<?php echo $uid ?>">
<?php foreach($data as $key => $value): ?>
<span id="span-<?php echo $key ?>">
<?php echo $value ?>
</span>
<?php endforeach; ?>
</div>
jQuery is written in JavaScript and runs on the user's browser while PHP runs only on the server. You cannot convert something that runs on the browser to something that runs on the server. In other words, you cannot use AJAX functions such as post
Read this article
Related
So I'm trying to make a simple API, I have tried lots of different things. I want to show a random number on my JSON object and a token to go with it, then store both in a database. I don't want this to happen when you visit the webpage I want the data to get sent to a DB and generated from a separate page.
The first step in this is getting data from a different file.
Here's what I tried first:
index.php:
<?php
$odds = rand(1, 100);
?>
<pre style="word-wrap: break-word; white-space: pre-wrap;">
{
"odds": <?php echo $odds;?>
}
</pre>
Here's the file I'm trying to get the data from:
<?php
$url = "https://flugscoding.com/random/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Riverside API");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if(!$response = curl_exec($ch)) {
echo curl_error($ch);
}
curl_close($ch);
$data = json_decode($response, true);
echo $data->odds;
?>
Error:
Notice: Trying to get property 'odds' of non-object in D:\xxamp\htdocs\random\perp.php on line 16
What I tried after:
index.php:
<?php
$values = array("odds"=>rand(1, 100));
echo json_encode($values);
?>
Here's the file I'm trying to get the data from:
<?php
$json = file_get_contents("index.php");
echo $json;
// echo $json->odds;
?>
Error:
It doesn't show any data or errors, just a blank screen.
Does anyone have any solutions to this problem? I'm trying to make a provably fair system for a friend.
You're really close in both your examples:
#1
$data = json_decode($response, true);
echo $data->odds;
You're passing true into json_decode which makes it an associative array, not an object. So you will need to get the odds with $data['odds']; instead.
#2
$json = file_get_contents("index.php");
echo $json;
file_get_contents can either fetch a local file or remote file. In this case, you're passing a local file, so $json is the contents of the PHP code.
You can either:
(a) redesign it so that index.php is a function, and you can call the function from different files
(b) call index.php remotely and parse the results
a. Create a function in index.php that can get the odds:
<?php
function getData() [
return array("odds"=>rand(1, 100));
}
Then, in another file, you can:
<?php
require_once 'index.php';
$data = getData(); //now you have access to $data['odds'];
b. Call index.php remotely, like this:
$json = file_get_contents("http://localhost/index.php");
print_r(json_decode($json, true));
is there a way in PHP, perhaps with an external library, to stream results from an API that responds with JSON data?
For instance I have the following code to get the data:
$resultsAPI = "https://www.example.com/api/results.json?
app_id=$app_id&token=$token&page=1&limit=10";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resultsAPI);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json;api_version=2' ));
$resp = curl_exec($curl);
curl_close($curl);
$results = json_decode($resp, true)['results'];
foreach ($results as $key=>$resultImage) {
$resultImage= "$resultImage[images]?app_id=$app_id&token=$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resultImage);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($curl);
curl_close($curl);
$image = json_decode($resp, true);
$results[$key]['image1'] = $image['image1'];
}
echo '<div class="card"><ul>';
foreach ($results as $result) {
echo '<li>';
echo '<span><p>'.$result['title'].'</p></span>';
echo '<span><p>'.$result['description'].'</p></span>';
echo '<span><img src="'.$result['image1'].'"></span>';
echo '</li>';
}
echo '</ul></div>';
It can take some time to load all data because it is going to loop over some large files. Is it possible to start streaming the results when it has the first data?
In the image below it shows what I am trying to explain. The data is being loaded in to the skeleton one by one:
Any thoughts on this would be very helpful and or if it is possible at all.
I think you're run into the wrong direction.
HTML begin to render after load all the html. So fetch the html with stream is not work for you.
In the demo, it just load a simple html page. Then load the other parts of the page with something like ajax. Each time a part loaded then render it.
Why not merge the the foreach loops? I haven't tested this but items should be echoed very iteration.
echo '<div class="card"><ul>';
foreach ($results as $key=>$result) {
$resultImage= "$result[images]?app_id=$app_id&token=$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resultImage);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($curl);
curl_close($curl);
$image = json_decode($resp, true);
$results[$key]['image1'] = $image['image1'];
echo '<li>';
echo '<span><p>'.$result['title'].'</p></span>';
echo '<span><p>'.$result['description'].'</p></span>';
if(isset($image['image1'])){
echo '<span><img src="'.$result['image1'].'"></span>';
}
echo '</li>';
}
echo '</ul></div>';
I hope that helps
As Kris Roofe said you may need ajax or axios call to output your data with some effects and animations exactly like the image that you attached in your question, your application need to be rendered first with all of it's HTML tags and assets like CSS and Javascript files then you can output some data and show it with animations by using ajax or axios and ofcurse you have more control over your data streaming in this case by using ajax or axios in client-side but you can also do it in your server-side but typically I prefer to do these things in client-side.
by the way if you insist to doing this in this way you can use flush() and ob_flush() to immediately output your data before the while loop ends.
Someone has already mentioned this in php official documentation link. You can check and read full documentation about output buffering and these methods.
You should merge your two foreach loops together and then add these two methods add the end of your loop so it will make it to output your data immediately after each loop iterate.
foreach($results as $key=>$resultImage){
//fetch images data such as title, description, and image itself in this loop
// and aslo echo your html tags in here.
// echo '<li>';
// echo '<span><p>'.$result['title'].'</p></span>';
// echo '<span><p>'.$result['description'].'</p></span>';
// echo '<span><img src="'.$result['image1'].'"></span>';
// echo '</li>';
}
I wrote some comments in your for loop to show you that you should merge your loops together, because you are trying to initialize $results variable in your first loop, and then after finishing that loop you are iterating in $results variable to show output data. so you can't output data immediately with two loops in here because your second loop depends on first one and it will not start iterating until the first one finishes. check this little code that I wrote to demonstrate the usage of these two methods:
$curl = curl_init();
for($i=0;$i<5;$i++){
curl_setopt($curl, CURLOPT_URL, 'example.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
$output = json_decode($output);
foreach($output as $key=>$value){
echo 'key: '.$key;
echo '$value: '.$value;
ob_flush();
flush();
}
}
I would highly recommend you to read about output buffering to implement it correctly in your projects.
I hope this could help you.
HTML starts rendering when some of it arrives. It does not have to be complete source. In PHP you can "send what you already echoed" via ob_flush() and flush() calls. This way it will immediately display in the browser. This can be paired with JSON stream parsing using for example halaxa/json-machine, so the result can look something like this:
<?php
echo '<div class="card"><ul>';
foreach (JsonMachine::fromStream($jsonStreamResource) as $result) {
echo '<li>';
echo '<span><p>'.$result['title'].'</p></span>';
echo '<span><p>'.$result['description'].'</p></span>';
echo '<span><img src="'.$result['image1'].'"></span>';
echo '</li>';
ob_flush();
flush();
}
echo '</ul></div>';
Fetch limited records while rendering html first load. Once page load fully then call a ajax function which will fetch next page rows. Definitely it is the tested method.
<script>
var items = [{item1}, {item2}];
$(document).ready(function() {
$.each(items, function(index, item) {
$('.card').append('<li>'+ '<span><p>'+item['title']+'</p></span>' +
'</li>');
});
});`enter code here`
</script>
OR
You may call ajax function for first page records after html rendered fully.
How do I load a JSON object from a file with ajax?
I want to pass a string from one PHP file to another using $_GET method. This string has different value each time it is being passed. As I understand, you pass GET parameters over a URL and you have to explicitly tell what the parameter is. What if you want to return whatever the string value is from providing server to server requesting it? I want to pass in json data format. Additionally how do I send it as Ajax?
Server (get.php):
<?php
$tagID = '123456'; //this is different every time
$tag = array('tagID' => $_GET['tagID']);
echo json_encode($tag);
?>
Server (rec.php):
<?php
$url = "http://192.168.12.169/RFID2/get.php?tagID=".$tagID;
$json = file_get_contents($url);
#var_dump($json);
$data = json_decode($json);
#var_dump($data);
echo $data;
?>
If I understand correctly, you want to get the tagID from the server? You can simply pass a 'request' parameter to the server that tells the server what to return.
EDIT: This really isn't the proper way to implement an API (like, at all), but for the sake of answering your question, this is how:
Server
switch($_GET['request']) {
case 'tagID';
echo json_encode($tag);
break;
}
You can now get the tagID with a URL like 192.168.12.169/get.php?request=tagId
Client (PHP with CURL)
When it comes to the client it gets a bit more complicated. You mention AJAX, but that will only work for JavaScript. Your php file can't use AJAX, you'll have to use cURL.
$request = "?request=tagID";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '192.168.12.169/get.php' . $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '3');
$content = trim(curl_exec($ch));
curl_close($ch);
echo $content;
EDIT: added the working cURL example just for completeness.
Included cURL example from: How to switch from POST to GET in PHP CURL
Client (Javascript with AJAX)
$.get("192.168.12.169/get.php?request=tagId", function(data) {
alert(data);
});
I know this has been done to death.
but i am really strugling
I have put together a webservice that generates a json,
i can and understand this bit in
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://marcom.domain.com/corp/pub.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
this will dump out on the page [{"Torders":"3222","name":"john"},{"Torders":"579","name":"Kevin"}]
their is 5 in total but for this i am keeping it simple
I don't understand how to get these as an array so that my end result is a list
of name and Torders
the rendered html would be something like this
<li>john 3222</li>
<li>Kevin 579 </li>
please don't send me to php manual page for json decode cause i am strugling to understand this.
thank you
<?php
$json = file_get_contents('http://marcom.domain.com/corp/pub.php');
$people = json_decode($json);
?>
<ul>
<?php foreach ($people as $person): ?>
<li><?=$person->name?> <?=$person->Torders?></li>
<?php endforeach; ?>
</ul>
If you want to use curl, you'll want to set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); and get the return from curl_exec()
U need to use json_decode()
$j = '[{"Torders":"3222","name":"john"},{"Torders":"579","name":"Kevin"}]';
$data = json_decode($j,true);
//print_r($data);
echo '<ul>';
foreach($data as $key=>$val){
echo '<li>'.$val['name'].' '.$val['Torders'].'</li>';
}
echo '</ul>';
I have this bit of javascript:
var jsonString = "some string of json";
$.post('proxy.php', { data : jsonString }, function(response) {
var print = response;
alert(print);
and this bit of PHP (in proxy.php):
$json = $_POST['json'];
//set POST variables, THIS IS WHERE I WANT TO POST TO!
$url = 'http://my.site.com/post';
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, "data=" . urlencode($json));
//execute post (the result will be something like {"result":1,"error":"","pic":"43248234af832048","code":"234920348239048"})
$result = curl_exec($ch);
$response = json_decode($result);
$imageHref = 'http://my.site.com/render?picid=' . $response['picid'];
//close connection
curl_close($ch);
echo $imageHref;
I am trying to post data to an external site using a proxy. From there, I append the picid that the site responds with and append it to the URL to get the image URL.
Am I missing something here? I am not getting anything in response and it seems like my data is not even being posted (when I try echo $json after the first line in proxy.php, I get an empty string). Why am I not able to echo the JSON? Is my implementation correct?
Thanks!
In your Javascript code, you are using this :
{ data : jsonString }
So, from your PHP code, should you not be reading from $_POST['data'], instead of $_POST['json'] ?
If necessary, you can use var_dump() to see what's in $_POST :
var_dump($_POST);
Edit after the comment : if you are getting a JSON result such as this :
{"result":1,"error":"","pic":"43248234af832048","code":"234920348239048"}
This is a JSON object -- which means, after decoding it, you should access it as an object in PHP :
$response = json_decode($result);
echo $response->pic;
Note : I don't see a picid element in that object -- maybe you should instead use pic ?
Here too, though, you might want to use var_dump(), to see how your data looks like :
var_dump($response);
try this:
$json = $_POST['data'];
or even better do
var_dump($_POST);
to see what is actually in your post when you start