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