Search code examples
phpxmlimdb

Save Page As XML


I have made a script that generates an IMDB API link for a movie in XML.

Once this link is generated it will save to an XML file with its contents. The only issue is that the contents aren't saving.

Link generated:
http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple

PHP script:

   $url="http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple";

    $curl = curl_init();
    $data = fopen("text.xml", "w");
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FILE, $data);
    curl_setopt($curl,  CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec ($curl);

    if ( !$data ) {
    echo "No";
} else {
    $contents = curl_exec($curl);
    fwrite($data, $contents);
}

curl_close($curl);
fclose($data);

Solution

  • Instead of using file_get_contents, you can use CURL

    $ch = curl_init('http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    

    Now $response shall contains your XML. And you can do something like

    file_put_contents('filename.xml', $response);
    

    make sure that filename.xml is writable