4

我正在使用这个 curl 类来保存文件 -->

class CurlHelper
{
  /**
   * Downloads a file from a url and returns the temporary file path.
   * @param string $url
   * @return string The file path
   */
  public static function downloadFile($url, $options = array())
  {
    if (!is_array($options))
      $options = array();
    $options = array_merge(array(
        'connectionTimeout' => 5, // seconds
        'timeout' => 10, // seconds
        'sslVerifyPeer' => false,
        'followLocation' => false, // if true, limit recursive redirection by
        'maxRedirs' => 1, // setting value for "maxRedirs"
        ), $options);

    // create a temporary file (we are assuming that we can write to the system's temporary directory)
    $tempFileName = tempnam(sys_get_temp_dir(), '');
    $fh = fopen($tempFileName, 'w');

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_FILE, $fh);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
    curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
    curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
    curl_exec($curl);

    curl_close($curl);
    fclose($fh);

    return $tempFileName;
  }
}

    $url = 'http://graph.facebook.com/shashankvaishnav/picture';
$sourceFilePath = CurlHelper::downloadFile($url, array(
  'followLocation' => true,
  'maxRedirs' => 5,
));

上面的代码会给我 $sourceFilePath 变量中的临时 url 现在我想将该图像存储在我的图像文件夹中。我被困在这里......请帮助我......提前谢谢你。

4

4 回答 4

18

有一个非常简单的选择:

$url = 'http://graph.facebook.com/shashankvaishnav/picture';
$data = file_get_contents($url);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

那时你甚至不需要任何其他东西。

或者如果file_get_contents被禁用(通常不是),这也应该有效:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://graph.facebook.com/shashankvaishnav/picture');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$fileName = 'fb_profilepic.jpg';
$file = fopen($fileName, 'w+');
fputs($file, $data);
fclose($file);

编辑:这不再可能,因为您不能再使用用户名进行 API 调用。在使用 Facebook API 而非用户名授权用户后,您必须使用(App Scoped)ID。

于 2012-11-02T08:58:24.013 回答
0
$filename = 'abcd.jpg';    
$to = 'image/'.$filename;
if(copy($sourceFilePath,$to))
echo 'copied';
else
echo 'error';
于 2012-11-02T09:01:58.517 回答
0
$fbprofileimage = file_get_contents('http://graph.facebook.com/senthilbp/picture/'); 
file_put_contents('senthilbp.gif', $fbprofileimage);
于 2012-11-02T09:09:41.027 回答
0
$profile_Image = 'http://graph.facebook.com/shashankvaishnav/picture';
$userImage = $picturtmp_name . '.jpg'; // insert $userImage in db table field.
$savepath = '/images/';
insert_user_picture($savepath, $profile_Image, $userImage);

function insert_user_picture($path, $profile_Image, $userImage) {
  $thumb_image = file_get_contents($profile_Image);
  $thumb_file = $path . $userImage;
  file_put_contents($thumb_file, $thumb_image);
}
于 2012-11-02T11:48:07.313 回答