I am trying to load the HTML source of a remote page into a string in PHP, using this awesome Galantis music video https://www.youtube.com/watch?v=5XR7naZ_zZA as an example.
I then want to search for a specific div id "action-panel-details" inside the source code and confirm when it's been found. With the code below, the entire page simply loads on the page that I'm running on my server.
Is this even possible with file_get_contents()? This is the code that loads the page, video and all:
<?php
$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');
if(preg_match("~action-panel-details~", $str)){
echo "it's there";
}
?>
I've tried using simplexml_load_file() also and end up with this error:
Warning: simplexml_load_string(): Entity: line 1: parser error : xmlParseEntityRef: no name in /page.php on line 5
Warning: simplexml_load_string(): ndow, document);</script><script>var ytcfg = {d: function() {return (window.yt & in /page.php on line 5
Warning: simplexml_load_string(): ^ in /page.php on line 5
Warning: simplexml_load_string(): Entity: line 1: parser error : xmlParseEntityRef: no name in /page.php on line 5
This is the code that's producing that:
<?php
$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');
$str = simplexml_load_string($str);
if(preg_match("~watch-time-text~", $str)){
echo "it's there";
}
?>
Any help is greatly appreciated.
Yes, you're very close. Basically, just scrap the part where you're trying to load this into XML since the page code is HTML and not XML.
$str = file_get_contents('https://www.youtube.com/watch?v=5XR7naZ_zZA');
if(preg_match("~watch-time-text~", $str)){
print "Match was found!";
}
else {
print "No match was found. :(";
}
This will display:
Match was found!
Unfortunately, I can't show you a demo since ideone.com
and codepad.org
aren't allowing me to use file_get_contents
, but this works from my own server.
If you run into situations where file_get_contents
is not allowed as I have, you can do as miglio said and use cURL to get the remote source. But the rest is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/watch?v=5XR7naZ_zZA');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch);
curl_close($ch);
if(preg_match("~watch-time-text~", $str)){
print "Match was found!";
}
else {
print "No match was found. :(";
}