Save Page As XML

て烟熏妆下的殇ゞ 提交于 2020-01-25 07:36:08

问题


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);

回答1:


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



来源:https://stackoverflow.com/questions/15308301/save-page-as-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!