Connecting to api through php (traileraddict) - php

I'm connecting to traileraddict.com. I can make the connection and display the 8 newest trailers in localhost. But when I load the page to the internet it won't display.
here is the exsample from trailer addict
<?php
$upcoming = simplexml_load_file("http://api.traileraddict.com/?featured=yes&count=8");
foreach($upcoming->trailer as $x => $updates)
{
echo $updates->title;
echo '<br>';
echo '<span style="font-size:x-small">Source: TrailerAddict</span>';
echo '<br>';
//now echo the embedded trailer
echo $updates->embed;
echo '<br><br>';
}
?>
And here is the error message I receive when I load it into the server.
Warning: simplexml_load_file(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Warning: simplexml_load_file(http://api.traileraddict.com/?featured=yes&count=8): failed to open stream: no suitable wrapper could be found in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://api.traileraddict.com/?featured=yes&count=8" in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 3 Notice: Trying to get property of non-object in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 5 Warning: Invalid argument supplied for foreach() in /heima/sth132/.public_html/Lokaverkefnireal/php/trailers.php on line 5
Bare in mind that don't have access to the server witch host's the site and I have also checked out related subject on stack, witch I don't under stand at all. So if you could help me that would be fantastic

That is because the server's configuration prevents it from directly opening URLs from simplexml_load_file(), so you should first download the page using Curl :
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://api.traileraddict.com/?featured=yes&count=8");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// load the previously downloaded XML page
$upcoming = simplexml_load_string($output);
// continue as usual (foreach $upcoming...)

Related

Parse XML file from url using php

I want to parse xml file from another server.
<?php
$xml = simplexml_load_file('http://example_page.com/api/test.xml');
?>
And this code work only if this file is on this same server as page, but I have got xml file on another server.
Warning from webpage:
Warning: simplexml_load_file(http://example_page.ugu.pl/api/test.xml) [function.simplexml-load-file]: failed to open stream: Connection refused in /virtual/z/y/example_page.ugu.pl/index.php on line 14
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://example_page.com/api/test.xml" in /virtual/z/y/example_page.ugu.ugu.pl/index.php on line 14
P.S. allow_url_fopen is still on.
Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file() doesn't follow redirects...
So the solution would be to use file_get_contents() or cUrl...
As file_get_contents() is easier, I am showing that only...
The code would have to be something like:
<?php
$xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));
?>
More:
<?php
$xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));
?>
^ That too, totally works!
You should use CURL (if is active).
$url = "http://www.you-web-here.com/";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$xmlString = curl_exec($curl);
PHP Documentation:
Note:
Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you.
So. which version are you used?
Or, you can try like this:
$xml_content = file_get_content('http://example_page.com/api/test.xml');
xml = simplexml_load_string($xml_content);

PHP file_get_contents problems with $_GET

I have a problem with my website. My website gets a variable and returns a String. What I mean by that is that if you visit my website the code will me only a 3 digit String eg FEG, PSJ, FGT, HJK. I want to take this String using the file_get_contents function. But when I do $con = file_get_contents("http://website.com/test.php?name=George"); it gives me this error
Warning: file_get_contents("http://website.com/test.php?name=George"): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\xampp\htdocs\test.php on line 2
Example using cURL instead:
<?php
$ch = curl_init("http://website.com/test.php?name=George");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

XML Error when I try to do simplexml_load_file

I am trying to turn a XML data from another website into arrays in my program. This is what I have written so far:
<?php
$rss = 'http://headlines.yahoo.co.jp/rss/asahik-dom.xml';
$xml = simplexml_load_file($rss);
var_dump($xml);
?>
However, when I try to load the php page, it comes up with this error:
Warning: simplexml_load_file(http://headlines.yahoo.co.jp/rss/asahik-dom.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://headlines.yahoo.co.jp/rss/asahik-dom.xml" in /home/www2/it32.lady2.itall.co.jp/www/yxml.php on line 11
bool(false)
FYI the $xml = simplexml_load_file($rss); is line 11.
Which part of my code has gone wrong?
Please help.
Please try file_get_contents() to load file and then use SimpleXMLElement to parse it.
Try
$rss = file_get_contents('http://headlines.yahoo.co.jp/rss/asahik-dom.xml');
$xml = new SimpleXMLElement($rss);
print_r($xml);
NOTE allow_url_fopen must be enabled in php.ini

Simple HTML Dom error when site doesn't exist

My script try to open every site from a file read line by line but when try to parse a site which doesn't exist the script stop and give me these error:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76
Warning: file_get_contents(http://www.thissitedoesntexist.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76
Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\test2.php on line 11
How can I fix it ? How can I make it to run even if a site doesn't exist... to read a new line from my file(which means to read another site and then to read it) Also the scrip stop when receive errors like 404, 403, etc.
I would do this, to check for the error codes ---
foreach ($sites as $site) {
$ch = curl_init('http://'.$site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
curl_exec($ch);
// Check if any error occurred
$info = curl_getinfo($ch);
if($info['http_code'] != 200) {
continue;
}
//rest of loop, here --
}
You could even do something different depending on the error code you get with a case-switch --
According to the documentation, file_get_contents() returns FALSE on failure.
So, check what it returns to be sure that the site exists before trying to parse the returned content. If it doesn't exist, iterate to the next line in the file and keep continue the process:
if($file = file_get_contents("http://www.thissitedoesntexist.com")) {
// Parse file here
// Then continue reading the file by
// starting at the next line.
continue;
}
Reference:
file_get_contents()

YouTube XML Feed Error

I've got a page on a website that's pulling my favorites feed from YouTube and embedding them into site.
The problem is it's working 80% of the time, but the other 20% of the time i'm getting errors on the page - nothing in the code is changing to cause this, so i'm wondering what might be causing this, or if there's a better way to do what i'm doing...
The Error I'm gettings is a 403 Forbidden when retrieving the XML feed... here's what it looks like (note: the line numbers won't match exactly, because i've simplified the code sample below.
The XML feed in question is here:
https://gdata.youtube.com/feeds/api/users/umarchives/favorites
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/users/umarchives/favorites) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/umarchives/favorites" in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: Invalid argument supplied for foreach() in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 47
Here's the code i'm using:
<?php
// set feed URL
$YouTubeUsername = "umarchives";
$feedURL = "https://gdata.youtube.com/feeds/api/users/".$YouTubeUsername."/favorites";
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->content->attributes();
$videoURL = $attrs['url'];
$videoURL = preg_replace('/\?.*/', '', $videoURL);
$videoURL = str_replace("/v/","/embed/",$videoURL);
$videoTitle = $media->group->title;
echo "<iframe class='youtube-player' width='300' height='225' src='$videoURL'></iframe>\n";
echo "<br>\n";
}
?>
You should be validating the result of $sxml = simplexml_load_file($feedURL); per the Google error validation docs. Then you can print out the actual message that comes along with the 403 code, or possibly decide to retry the request. If it's a random occurrence my guess is a quota limit issue, but the actual error information will likely tell you exactly what you want to know.
MYUSERNAME is not a valid username. Add your own youtube username!
When I call your feed URL in browser (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) I receive this error:
Favorites of requested user are not public.
Make your feed public, and the failure should be gone.

Categories