$.get in PHP - Turn JS into PHP
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I can do this in JS but how do I do exactly the same in PHP?

https://jsfiddle.net/Jacknumpty/5sq14rjp/17/

$(document).ready(function() {
sceneQueryGetAnswer();
});

function sceneQueryGetAnswer() {
$.get("https://digitaldraping.picarioxpo.com/xpo/api/v2/scenes/1737?api_key=f6c9f58215234710916fe8c4f2c1affa",
function(data) {
var myOne = data.id;
$("#one").html(myOne);
for (var x = 0; x < data.properties.length; x++) {
if (data.properties[x].name == "Price") {
var myTwo = (data.properties[x].value);
$("#two").html(myTwo);
}
}
});
}

you can't simply convert js into php, js works client side, php server side; but you can replicate that feature via curl. The user Kehet has already provided a good solution.
Stefano Balzarotti 4 months ago
awarded to Kehet

Crowdsource coding tasks.

2 Solutions

Winning solution

You could use curl like this

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://digitaldraping.picarioxpo.com/xpo/api/v2/scenes/1737?api_key=f6c9f58215234710916fe8c4f2c1affa");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$json = json_decode($output);

echo $json->id . "<br>\n";

foreach ($json->properties as $property) {
    if($property->name == "Price") {
        echo $property->value . "<br>\n";
    }
}
Thank you exactly what I wanted!
Jacknumpty 4 months ago

<?php
$api = "https://digitaldraping.picarioxpo.com/xpo/api/v2/scenes/1737?api_key=f6c9f58215234710916fe8c4f2c1affa";
$result = file_get_contents($api);
$arr = json_decode($result, true);
$price = array();
foreach ($arr['properties'] as $prop){
if($prop['name'] == "Price"){
$price[] = $prop['value'];
}
}
echo json_encode(array('id' => $arr['id'], 'prices' => $price));
?>

This returns a JSON encoded array as PHP can't alter Page elements

View Timeline