I tried to work with basic, few param. JSON code and there wasn't any problem with that.
BUT now i need work with this "advanced" JSON and I am kinda lost
{
"code":"success",
"username":"x",
"nodes":[
{
"id":"68",
"time":987
},
{
"id":"69",
"time":987
}
]
}
When i tried to get the values into PHP variable with previou code, I wasn't able to get ID and TIME, CODE and SUCCESS isn't problem.
PHP code I used:
$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = #file_get_contents($url);
$details = json_decode($json, TRUE);
// print_r($details);
echo $details[code];
Thank you guys!
<?php
$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = #file_get_contents($url);
$details = json_decode($json, TRUE);
if (is_array($details['nodes'])) {
echo "node count: " . count($details['nodes']) . "<br />";
foreach ($details['nodes'] as $node) {
echo "id: " . $node['id'] . "<br />";
echo "time: " . $node['time'] . "<br />";
}
}
Related
I already made my personal single thread proxy checker using php,but I couldnt make it multi-thread,some days ago,I found one checker using multi-thread on github,can someone help to change it to save the good proxies into a file (ip:port format)?
https://raw.githubusercontent.com/samuel-allan/FastProxyChecker/master/checker.php
What i have tried:
original line 91:
echo json_encode($arr);
changed to:
$json = json_decode($arr);
$good_proxie = $json['arr']['result']['proxy']['ip'];
echo "$good_proxie";
I did not checked it, but think it'll work ^_^
function CheckMultiProxy($proxies, $timeout, $proxy_type)
{
$data = array();
foreach($proxies as $proxy)
{
$parts = explode(':', trim($proxy));
$url = strtok(curPageURL(),'?');
$data[] = $url . '?ip=' . $parts[0] . "&port=" . $parts[1] . "&timeout=" . $timeout . "&proxy_type=" . $proxy_type;
}
$results = multiRequest($data);
$holder = array();
foreach($results as $result)
{
$holder[] = json_decode($result, true)["result"];
}
$arr = array("results" => $holder);
foreach ($arr['results'] as $proxy) {
if ($proxy['success']) {
file_put_contents('YOUR_FILE_HERE', $proxy['proxy']['ip'].':'.$proxy['proxy']['port'].' '.$proxy['proxy']['type'].PHP_EOL, FILE_APPEND);
}
}
echo json_encode($arr);
}
I have a Api response in XML format.How can I get gps_x and gps_y for both elements.
$url="http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo";
$xmlinfo = simplexml_load_file($url);
print_r($xmlinfo);
echo $xmlinfo['gps_x']; // outputs nothing
echo $xmlinfo -> gps_x; // outputs nothing
How can I get gps_x and gps_y from above response?
I did this by getting the content from url then converting to json using exceptions handling and get the data from decoded json:
<?php
$myXMLData = file_get_contents("http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo");
$simpleXml = simplexml_load_string($myXMLData) or die("Error: Cannot create encode data to xml object");
$jsondata = json_encode($simpleXml) or die("Error: Cannot encode record to json");
$data = json_decode($jsondata, true);
$in = $data['items']['item'];
foreach ($in as $key => $value) {
echo "ID= " . $in[$key]['id'] . ", GPS-x = " . $in[$key]['gps_x'] . ", GPS-y = " . $in[$key]['gps_x'];
echo "<br/>";
}
?>
OUTPUT
ID= 2354292, GPS-x = 36.1065000000, GPS-y = 36.1065000000
ID= 2431066, GPS-x = 36.0949905151, GPS-y = 36.0949905151
If you want to take the data from XML directly:
<?php
$myXMLData = file_get_contents("http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo");
$simpleXml = simplexml_load_string($myXMLData) or die("Error: Cannot create encode data to xml object");
$in = $simpleXml->items->item;
foreach ($in as $key) {
echo "ID= " . $key->id;
echo ", GPS-x = " . $key->gps_x;
echo ", GPS-y = " . $key->gps_y . "<br/>";
}
?>
OUTPUT
ID= 2354292, GPS-x = 36.1065000000, GPS-y = 28.0684000000
ID= 2431066, GPS-x = 36.0949905151, GPS-y = 28.0860328674
Looking at the print_r() output, it show that the gps_x & gps_y are part of an item and not directly under the xmlinfo object.
Here is the code that will do the job:
$url = "http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo";
$xmlinfo = simplexml_load_file($url);
if ($xmlinfo->items && $xmlinfo->items->item) {
$item = $xmlinfo->items->item;
print $item->gps_x . "\n";
print $item->gps_y . "\n";
}
$url="http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo";
$xmlinfo = simplexml_load_file($url);
foreach ($xmlinfo->items->item as $item) {
//echo "<pre>";print_r($item);
echo "<br />". $item->gps_x;
echo "<br />". $item->gps_y;
}
I'm new to PHP and try to echo out some data from json, but I got stucked in this.
It shows non of the data, but the data is there. The var_dump() shows it to me.
Probably I don't use the array correctly, but I can't find what's wrong. I've this code,
I request some data which I gathered with knockout.js
Knockout gives me a json (as show below)
$json = $_REQUEST[seats];
echo 'requested data raw '. "<br>".$json;
$data = json_decode($json, true);
echo "<br>".'var_dump '. "<br>";
var_dump($data);
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optieName . "<br>";
echo "prijs = " . $optie->prijs . "<br>";
}
This is my JSON:
[
{
"name": "Naam 1",
"optie": {
"optieName": "Make_up",
"prijs": 9.95
},
"PrijsFormated": "Euro: 9.95"
},
{
"name": "Naam 2",
"optie": {
"optieName": "Handverzorging",
"prijs": 12.95
},
"PrijsFormated": "Euro: 12.95"
}
]
You should use such loop:
foreach ($data as $optie ) {
echo "name = " . $optie['name'] . "<br>";
echo "optie = " . $optie['optie']['optieName'] . "<br>";
echo "prijs = " . $optie['optie']['prijs']. "<br>";
}
Because using json_decode() with second parameter as true you have created associative array - documentation.
If you would like to access data as object, you should use:
$data = json_decode($json);
instead of
$data = json_decode($json, true);
and then you should use the following loop:
foreach ($data as $optie ) {
echo "name = " . $optie->name . "<br>";
echo "optie = " . $optie->optie->optieName . "<br>";
echo "prijs = " . $optie->optie->prijs. "<br>";
}
If I wanted to echo the link of a general thread on 4chan.
Here's what I was thinking of, however I have no idea on what to do
$jsonurl = "http://a.4cdn.org/vg/catalog.json";
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach( $json_output as $no )
{
if(strpos(sub, 'DOTA')) { //Not sure how I would do this
//echo the "no" of it in the json
}
}
Had to look at the JSON data to see what you were looking for. Give this a try
$jsonurl = "http://a.4cdn.org/g/catalog.json";
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output as $page) {
foreach($page->threads as $thread) {
if (isset($thread->sub)) {
$sub = $thread->sub;
$no = $thread->no;
echo $sub . ', Thread Number: ' . $no . '<br />';
/*
if (strpos($sub, 'DOTA') !== false) {
echo 'Found DOTA!!! Thread Number is: ' . $thread->no;
}
*/
}
}
}
You should study the JSON data that you receive in order to know how to extract the data. This is one way of doing it:
foreach( $json_output as $page ) {
foreach ($page->threads as $t) {
echo 'http://boards.4chan.org/vg/res/' . $t->no . ', ';
}
}
http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0
These are player sales from a browser game. I want to save some fields from these sales. I am fetching that xml with curl and storing on my server. Then do the following:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/transfer');
print_r($items);
foreach($items as $item) {
echo $item['buyerTeamname'], ': ', $item['sellerTeamname'], "\n";
}
The array is empty and i cant seem to get anything from it. What am i doing wrong?
There is no reason to use cURL or XPath for that. You can do
$url = 'http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0';
$transfers = new SimpleXMLElement($url, NULL, TRUE);
foreach($transfers->transfer as $transfer) {
printf(
"%s transfered from %s to %s\n",
$transfer->playerName,
$transfer->sellerTeamname,
$transfer->buyerTeamname
);
}
Live Demo
You forgot a slash in your xpath:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('/*/transfer');
print_r($items);
foreach($items as $item) {
echo $item->buyerTeamname, ': ', $item->sellerTeamname, "\n";
}
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
Is this what you want?