Print out GData title in PHP [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this link
http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1
That gives me the latest video from GudjoDaneel but I'd like to print our this title inside a PHP file
<title type='text'>The GD Project S3 | NEVER GIVE UP! | Division 1</title><content type='text'>
I'd appreciate it if someone could help me where to begin. And what I could look up.

I'd suggest looking up SimpleXML. It's easy to use once you get the hang of it, and you can get the title in just four line:
$url = 'http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
$title = $xml->entry->title;
Do note, though, that $title is a PHP object in this case. If you echo it straight away, it'll be reinterpreted as a string, and everything will be alright. If you plan on doing anything else with it, you'll need to cast it as a string, like this:
$title = strval($title);

Related

is there a way to reestructure file_get_contents output? php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Im triyng to take some information from a json file,
the code in that page is like this
{"asignaciones":[{"fecha":"LUNES
29/07","horaEntrada":"18:30","horaSalida":"22:30","tienda":" BK Villa
Urquiza"}],"fechaConsulta":"29/07/2019 17:27","legajo":"28907"}
i want to take "29/7", "18:30", "22:30".
For now, i can print all the code in my page, but i want to take only those numbers, there is a way with:file_get_contents?
i'm trying to learn a little more php sorry if this question is oviously simple.
my code now:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
echo $content
?>
It looks like the content is a json... Start from this:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
$content = json_decode($content);
print_r($content);

Get src and content from a variable [duplicate]

This question already has answers here:
Grabbing the href attribute of an A element
(10 answers)
Closed 9 years ago.
I'm new to php and and i need help with something.
I need to divide the image and the content from this variable. It has a image and the description.
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
I know this is something simple but please help me. I need the image in a variable and the content with the link in another..
Thanks.
As #Barmar says, you should use a DOM parsing library.
You may find very useful this: http://simplehtmldom.sourceforge.net
Its use is similar to a jQuery parsing, and you only have to read doc to know how to use it (very good examples => http://simplehtmldom.sourceforge.net/manual.htm)
Example:
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
$html = str_get_html($content);
$image = $html->find('img');
$links = $html->find('a');
And you'll have what you want in $image and $links, :D

How to read in XML strings as variables? [duplicate]

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.
You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

PHP get a div from page x [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I wanna extract some html from page Y
for example site is
<head>xxxx</head>
<body>....<div id="ineedthis_code"> </div> ...</body>
it is possible to do this file_get_contents ?!
i need only that div nothing else
Without using a special library (which is the best way in most cases), you can use the explode-function:
$content = file_get_contents($url);
$first_step = explode( '<div id="YOUR ID HERE">' , $content ); // So you will get two array elements
$second_step = explode("</div>" , $first_step[1] ); // "1" depends, if you have more elements with this id (theoretical)
echo $second_step[0]; // You will get the first element with the content within the DIV :)
Please note, it's only an example without error handling. It also works onlny on a special case; not if the html structure ist changing. Even simple spaces can break this code. So you should better use a parsing library ;-)

Tags still remaining after using simplexml_load_string [duplicate]

This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>

Categories