0

我目前正在尝试在我的班级中创建一个从外部服务器获取数据的函数。我可以使用 CURL 获取数据,但我不想使用 CURL 直接将其存储在文件中。这很难解释,所以我会告诉你。

这是我获取图像的功能:

function getCharacterPortrait($CharID, $size){

    $url = "http://image.eveonline.com/character/{$CharID}_{$size}.jpg";
    $ch = curl_init($url);
    curl_setopt_array($ch, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING       => "",
        CURLOPT_AUTOREFERER    => true,
    ));
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;

}

所以,我想从这里做的是获取$data我假设的原始图像,并将其存储在.jpg指定文件中。我使用 CURL 找到了类似的解释,但 CURL 用于直接存储它。我想要调用函数以获取图像以存储文件的脚本。

对不起,如果我有点混乱,但我认为你明白我所说的前提。如果需要更多解释,请这样说。

4

3 回答 3

0

这个怎么样?

$a = implode('',@file('http://url/file.jpg'));
$h = fopen('/path/to/disk/file.jpg','wb');
fwrite($h,$a);
fclose($h);
于 2012-12-03T11:39:45.013 回答
0

用它

$url = "http://image.eveonline.com/character/{$CharID}_{$size}.jpg";
$local_path = "myfolder/{$CharID}_{$size}.jpg";
$file = file_get_contents($url);
file_put_contents($file, $local_path);
于 2012-12-03T18:34:08.823 回答
0

Just write all the data cURL gave you to a file with file_put_contents() for example?

[your curl code...]
file_put_contents('localcopy.jpg', $data);


Edit:

Apparently there is also a cURL option to download to a file like:

$fp = fopen($path, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);

Found at http://www.phpriot.com/articles/download-with-curl-and-php

于 2012-12-03T11:44:39.920 回答