Parsing XML from a Web Page with PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I heard that you can parse information from a web page with SimpleXML.
How can I get the the Usernames and ID's from the following page and display them on my website like:
Username:Napolien
Id:2202591
The URL to the website from which I want to get the information is:
http://api.roblox.com/users/3/friends

Your api returns json, you can decode it with json_decode, i have also include how to traverse through the json objects included in the feed.
$json = file_get_contents("http://api.roblox.com/users/3/friends");
$obj = json_decode($json);
for ($i=0; $i < count($obj); $i++) {
echo $obj[$i]->Id;
echo $obj[$i]->Username;
}

The data is actually JSON. You can get it like this :
$jsonString = file_get_content('http://api.roblox.com/users/3/friends');
$data = json_decode($jsonString, true);
$username = $data[14]['username']; // 14 is your desired index
$id = $data[14]['id'];

Related

How to print a String from a json file in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've a problem.
I want to print out a string the string is in this JSON file with a php scipt:
http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy
it's: data / description
i hope you understand what i mean.
Use jsondecode php function.
$url = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$jsondata = file_get_contents($url); //json data from url
$data = json_decode($jsondata,1); // convert json data to array
echo $data['data']['description']; // access data using keys of array, mapped to properties in json
using json_decode
$json_uri = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$json = file_get_contents($json_uri);
$data = json_decode($json,1); // convert json data to array
$count = 1;
foreach ($data['data'] as $key => $info) {
echo $count++.' - '.$info['description'].'<br>';
}

Access property of a PHP object [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm passing in an object using an ajax function. The object looks like this:
{"label":"1","number":2}
Once the object reaches the server I'm using PHP to json_decode it.
After it's decoded how can I start accessing the properties of the object? For example I want to retrieve the value of label - how can this be done?
$myobj = json_decode($JSON);
print $myojb->label;
You might want to read about Classes and Objects in the manual. An alternative syntax would be decoding the JSON to an array and then accessing it via the key.
$myarr = json_decode($JSON, TRUE);
print $myarr['label'];
You can access it like this;
$json = json_decode($input);
echo $json->label;
$input=json_decode(your json);
echo $input->label;
you can do it this way....
from jquery..
var a= {'label':'1','number':'2'};
$.ajax({
data : {data:JSON.stringify(a)},
})
from php side
if(isset($_POST['data']))
{
$data = $_POST['data'];
$data = json_decode($data,true);
echo $data['label']; //will print label here
}
this is full example of ajax request with json to php...
might help you...

convert short youtube url to full url [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi all i am looking for a simple way to check if a string equals an url like this:
http://youtu.be/WWQZ046NeUA
To convert it to a proper youtube url like this:
http://www.youtube.com/watch?v=WWQZ046NeUA
If not to leave it alone, what's the simplest way to do it in php?
You can use this preg_replace call:
$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
str_replace should work wonders.
$url = ''; //url you're checking
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url,$yturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}

Getting value from JSON [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am struggling to get pull the correct values
I am trying to get the following
packages>items then loop through the ids
{"id":1,"title":"Barnsley","slug":"barnsley","packages":[{"id":1,"title":"Group PACKAGE 1","items"
{"id":9,"title":"Guiness","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":10,"title":"John Smith","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":22,"title":"Chicken Cheese and Bacon Melt","band_a":4.5,"band_b":4.75,"band_c":5.0,"band_d":5.25,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null}],"additional_items":null,"event_types":null}]}
Your JSON isn't valid. You're missing two characters :[ that declare the items array. After fixing the JSON (check it on jsonlint.com) you can iterate over the item IDs like so:
$obj = json_decode($str);
foreach($obj->packages[0]->items as $item)
{
echo $item->id;
}
If there could be more than one package you can iterate those too:
foreach($obj->packages as $package)
{
foreach($package->items as $item)
{
echo $item->id;
}
}

How do i extract a number from a url using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}

Categories