0

I'm trying to save a local copy of an xml file, and then open it with simple xml, but i'm getting some errors.. here's my code:

$feedURL = "https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites";

//$xml = file_get_contents("$feedURL");
$xml = file_get_contents($feedURL);
file_put_contents("video.xml", $xml);


// read feed into SimpleXML object
//$sxml = simplexml_load_file($feedURL);
$sxml = simplexml_load_file('video.xml');

The error i'm getting is as follows:

Warning: file_get_contents(https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites) [function.file-get-contents]: failed to open stream: Result too large in D:\wamp\www\videos2.php on line 48

I'm not sure why it would be too large of a result, it only returns 6kb of xml. what am i doing wrong?

Update: This is running on a windows platform using WAMP server - not ideal, but i'm stuck with it.

Update 2: I've tried using curl and fwrite to achieve a similar result, as suggested below, but it won't write the xml file to the local server. It doesn't give me any errors though.

update 3: This is obviously a very specific problem with the hosting environment, but I'm not sure where to start looking for the problem. Using curl works great on a linux-based dev server, but is causing problems on this windows-based production server. An extra help in troubleshooting this issue would be most appreciated!

Dan
  • 3,750
  • 5
  • 24
  • 38
  • Why do you assume that the term "result" correlates to the size of the response? – hakre Sep 21 '11 at 17:11
  • I suppose it's foolish to assume that, but if I'm expecting the contents of a file as a result, and it says it's too large, it seems like a logical assumption. - if I've misinterpreted it, i would love it if you could set me straight. – Dan Sep 21 '11 at 17:21
  • From what I see in PHP source, such an error message ("Result too large") is not part of PHP (5.3.8). Maybe this is from the remote server you send the request too? – hakre Sep 21 '11 at 17:45

2 Answers2

2

Correct answer for the question:

It is possible you are having the same problem as of this question: CURL and HTTPS, "Cannot resolve host" (DNS-Issue)


Other Details:

You can use SimpleXML to load and save the xml data

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
$xml->asXML('video.xml');

I have tested the code above in a WAMP server and it works fine.

Update: If the above returns error message "[simplexmlelement.--construct]: I/O warning : failed to load external entity ...." It's possible that your server does not allow to include external data or the php file/script does not have the right permission.

Try the following:
1. echo the content of the xml file.

$xml = new SimpleXMLElement('https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites', NULL, TRUE);
echo htmlentities($xml->asXML());

If you managed to retrieved the xml content and print it to the browser, then your server is allowing to include external content and most likely the problem with the file permission. Make sure file/script have the right to create xml file.

If the above still does not work try using cURL.

function getPageContent($options)
{
    $default = array(
        'agent' => $_SERVER['HTTP_USER_AGENT'],
        'url' => '',
        'referer' => 'http://'.$_SERVER['HTTP_HOST'],
        'header' => 0,
        'timeout' => 5,
        'user' => '',
        'proxy' => '',
    );
    $options = array_merge($default, $options);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $options['url']);
    curl_setopt($ch, CURLOPT_HEADER, $options['header']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($options['proxy'] != '') {
        curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
    }
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
    curl_setopt($ch, CURLOPT_REFERER, $options['referer']);
    curl_setopt($ch, CURLOPT_USERAGENT, $options['agent']);
    if ($options['user'] != '') {
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, $options['user']);
    }

    $result = array();
    $result['content'] = curl_exec($ch);
    $result['info'] = curl_getinfo($ch);
    $result['error'] = curl_error($ch);

    curl_close($ch);

    return $result;
}

$result = getPageContent(array(
    'proxy' => '[ip or address]:[port]', // if needed 
    'user' => '[username]:[password]',   // if needed
    'url' => 'http://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites'
));

if (empty($result['error'])) {
    // ok

    // content of xml file
    echo htmlentities($result['content']);

    // file
    $filename = 'video.xml';
    // Open File
    if (!$fp = fopen($filename, 'wt')) {
        die("Unable to open '$filename'\n\n");
    }
    // write content to file
    fwrite($fp, $result['content']);
    // close file
    fclose($fp);

} else {

    // failed
    echo '<pre>';
    echo 'Error details;';
    print_r ($result['error']);
    echo '<hr />Other info:';
    print_r ($result['info']);
    echo '</pre>';
}
Community
  • 1
  • 1
satrun77
  • 3,202
  • 17
  • 20
  • I'm getting the following errors from that code: Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites" in D:\wamp\www\videos2.php on line 53 Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in D:\wamp\www\videos2.php:53 Stack trace: #0 D:\wamp\www\videos2.php(53): SimpleXMLElement->__construct('https://gdata.y...', 0, true) #1 {main} thrown in D:\wamp\www\videos2.php on line 53 – Dan Sep 26 '11 at 13:47
  • Alright, I tried echoing out the data, but i get the same I/O Error. If i save the contents of the feed manually to a local file it seems to work just fine. my php_info says that i should be able to include remote files, but i can't seem to get it work. I also tried your curl method and get the error: Error details;Could not resolve host: gdata.youtube.com; No data record of requested type - I appreciate all your help - are there settings on the server that i should look at trying to change? – Dan Sep 28 '11 at 15:44
  • It is possible you are having the same problem as of this question: http://stackoverflow.com/questions/1341644/curl-and-https-cannot-resolve-host – satrun77 Sep 28 '11 at 19:54
  • BINGO! I think you've nailed it. There seems to be a DNS issue with the Web Server that was preventing the URL from resolving. Thanks so much for this. If you wouldn't mind sticking this solution as a separate answer so it will be easier to find for others, I'll mark it correct and award the bounty! – Dan Sep 28 '11 at 20:10
  • Great it is all working :) I have updated the same answer just to keep the question with less answers. – satrun77 Sep 28 '11 at 21:27
1

Have you tried using curl and fwrite to get the contents and write them to a local file?

$ch = curl_init("https://gdata.youtube.com/feeds/api/users/manitobachildhealth/favorites");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);       
curl_close($ch);

fwrite("video.xml",$output);
jivinivan
  • 99
  • 9
  • I have tried it, but apparently this server doesn't have curl installed. its on a windows machine running WAMP server. I'll update the question to make note of that, since it's probably fairly relevant. – Dan Sep 21 '11 at 17:27
  • PHP has curl on windows. Just activate the library. – hakre Sep 21 '11 at 17:44
  • Ok i think i've got curl running now, but it doesn't seem to want to write the file using the example above, and it doesn't output any error messages... – Dan Sep 23 '11 at 16:08
  • I'm sure you already checked but make sure the file has write permissions and the path is correct. – jivinivan Sep 27 '11 at 23:25