PHP Download Facebook User Image Profile Error - php

help me to fix this problem
I want to download Facebook user image profile using PHP 5.3.27
I use this code, the image is downloaded but size 0 bytes and cannot be open.
first code:
function DownloadImage($url, $dest){
$curl = curl_init($url);
$fp = fopen($dest, 'wb');
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($fp);
}
try{
$social_id='1234';
DownloadImage('http://graph.facebook.com/'.$social_id.'/picture', '../user_image/normal/'.$social_id.'.jpg');
DownloadImage('http://graph.facebook.com/'.$social_id.'/picture?width=141&height=141', '../user_image/bigger/'.$social_id.'.jpg');
}
catch(Exception $errr){
echo $errr;
}
and this is the second code..
with the same result (0 bytes of size and cannot be open)
$url = 'http://graph.facebook.com/1234/picture';
$img = '../user_image/bigger/1234.jpg';
file_put_contents($img, file_get_contents($url));

As I see
http://graph.facebook.com/1234/picture
do redirect to:
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/t1.0-1/c47.7.85.85/s50x50/417599_10100236041078581_1583446385_a.jpg
Try to add:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
So your code may looks like:
function DownloadImage($url, $dest){
$curl = curl_init($url);
$fp = fopen($dest, 'wb');
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_exec($curl);
curl_close($curl);
fclose($fp);
}
try{
$social_id='1234';
DownloadImage('http://graph.facebook.com/'.$social_id.'/picture', '../user_image/normal/'.$social_id.'.jpg');
DownloadImage('http://graph.facebook.com/'.$social_id.'/picture?width=141&height=141', '../user_image/bigger/'.$social_id.'.jpg');
}
catch(Exception $errr){
echo $errr;
}

Related

How to solve cUrl corrupting file during download

I really, really need help as to how to solve this issue I'm having:
Using script:
<?php
$curl = curl_init();
$fp = fopen("somefile.zip", "w");
curl_setopt ($curl, CURLOPT_URL, "http://website.com/test.zip");
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_exec ($curl);
curl_close ($curl);
I have asked before, and no-one seems to have a solution as to how I solve this... If someone can even tell me why it's happening ie. is it file size, binary transfer etc. I can work with that!
The file ZIP file downloads and creates somefile.zip but the XML file within is partially corrupt.
Sample:
K#Teº22)dVTð¼ÜvØ
rÏ*HIê±dE*¬òPÜÊâR}ÝbJÉÂX:Î#z|Eª2Ér tk­2UÄOK¼É,·,­Ûs¦ê1Z°VÝk6Ù«ËGÝw©5Æ]ÛQcq¥¼½ØïÒÐ]êÈy¨ð¶Çùûü]ÛßþW¤ùâÝÀw|~§ïúÁ¸ÛHBq®*YtrÛÕiî$ /ñ¥n?è¶;_ò
É¡ä ç&ýOr óß)yÿ¤$+`~TÙAófHU ¢SÝvW¶¦xA5Å׶Ãrå<8^ÐË4w­ qz Ø«<Ñ"*ººÝ?èO^;ÃQûÉOÏÀ¾?ìw|Õ±¥©3w©Ýr£ ÃÊÀ ¿^Á^UÛLß_ôÜÎh4îÖWcíF^8¾ö÷ؼ¾¿`âX3Ûú^{ À<.Æ¡(±1f¢.¸®k/ìÝeÓçê'PAnÓõ¸K`TeQ÷b|'¥Ñ)1ÓãnsÞèàÎZ|ê*+kuw×cªëÇ:§$¤ã¸Î1ü±Úh6ÕÀQ¦©D4Âp4b{Èo¾
,4"R
Can you set CURLOPT_HEADER to 0 and try again?
Edit:
Or try this:
$url = 'http://website.com/test.zip';
$path = 'somefile.zip';
$ch = curl_init($url);
if($ch === false)
{
die('Failed to create curl handle');
}
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);

Check if a remote file is a valid mp3

Could you tell me a good way to check if a remote(http) file is a valid mp3, WITHOUT downloading the whole file?
Do not trust anything that comes from the server. MIME type may be fake. Even the filename. Just assume that you are expecting a MP3 file. Store the correct extension and verify it.
Well at least the header along with the correct extension to the filename. At worst they get a horrible noise in their ears and you get a complaint.
You can try
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://...music.mp3");
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$header = curl_exec($curl);
$info = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
curl_close($curl);
$mp3_mimes = array('audio/mpeg','application/mp3','audio/mpeg3','audio/x-mpeg-3');
if (in_array($info, $mp3_mimes)) {
// Somthign cooking
}
Another suggestion is download the file and validated by reading header information
$curl = curl_init();
$localfile = "local.temp";
unlink($localfile);
$file = fopen($localfile, 'w+');
curl_setopt($curl, CURLOPT_URL, "http://robtowns.com/music/blind_willie.mp3"); // nput
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $file);
curl_exec($curl);
curl_close($curl);
fclose($file);
$mp3 = new mp3();
if ($info = $mp3->get_mp3($localfile, true, false)) {
var_dump($info);
} else {
unlink($localfile);
// bad File
}
MP3Class
Check the filesize and if its >1mb validate... Not the answer but the best for now...

PHP cURL error The URL was not properly formatted

I am trying to do a simple cURL file upload from one server to another. The problem is I get Error #3 from the cUrl error codes: The URL was not properly formatted.
I have copied the url into my browser and logged onto the ftp site without a problem. I have also verified the proper formatting and searched the web and this site for an answer without any success.
Here's the code:
$ch = curl_init();
$localfile = '/home/httpd/vhosts/homeserver.com/httpdocs/admin.php';
echo $localfile; //This reads back to proper path to the file
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://username:password#199.38.215.1xx/');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'Upload error:'.$error_no ;//Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html';
}
echo $error;
I have also tried this:
curl_setopt($ch, CURLOPT_URL, 'ftp://199.38.215.1xx/');
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
I still get error #3.
Any ideas?
your remote URL needs to contain the path and name of the destination file, as shown in this example
<?php
// FTP upload to a remote site Written by Daniel Stenberg
// original found at http://curl.haxx.se/libcurl/php/examples/ftpupload.html
//
// A simple PHP/CURL FTP upload to a remote site
//
$localfile = "me-and-my-dog.jpg";
$ftpserver = "ftp.mysite.com";
$ftppath = "/path/to";
$ftpuser = "myname";
$ftppass = "mypass";
$remoteurl = "ftp://${ftpuser}:${ftppasswd}#${ftpserver}${ftppath}/${localfile}";
$ch = curl_init();
$fp = fopen($localfile, "rb");
// we upload a JPEG image
curl_setopt($ch, CURLOPT_URL, $remoteurl);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
// set size of the image, which isn't _mandatory_ but helps libcurl to do
// extra error checking on the upload.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$error = curl_exec($ch);
// check $error here to see if it did fine or not!
curl_close($ch);
?>

Download files from Megaupload with cURL PHP

I'm writing a script to download files from Megaupload onto my server. I'm using cURL on PHP, I have the login script which downloads the cookie file:
<?php
function login($username, $password){
$mega = curl_init();
curl_setopt($mega, CURLOPT_URL, "http://www.megaupload.com/?c=login");
curl_setopt($mega, CURLOPT_POST, true);
curl_setopt($mega, CURLOPT_POSTFIELDS, "login=1&redir=1&username=$username&password=$password");
curl_setopt($mega, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_setopt($mega, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/megaupload_cookie.txt");
curl_exec($mega);
curl_close($mega);
}
?>
and the downloading script:
<?php
include("megaupload_login.php");
login("username", "ps");
set_time_limit(0);
$url = "http://www.megaupload.com/?d=A428CAKH";
$fp = fopen("winch.zip", "w");
$dl = curl_init($url);
curl_setopt($dl, CURLOPT_COOKIEFILE, "megaupload_cookie.txt");
curl_setopt($dl, CURLOPT_FILE, $fp);
curl_exec($dl);
curl_close($dl);
fclose($fp);
?>
The problem is, the file doesn't download. All I get is a file named winch.zip with a size of 0 bytes. I think the program is actually downloading the login page, as when run the script the browser just shows the megaupload login page but the address is localhost. Any ideas on why this might not be working?
Try Following for your download part of code:
function download_file($link) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$filecontents = download_file("http://www.megaupload.com/?d=A428CAKH");
Hope it helps

curl_exec succeeds but the output file is empty

I wrote the PHP function below to download files and it works as expected.
However, when I try to download this file:
$url = 'http://javadl.sun.com/webapps/download/GetFile/1.7.0_02-b13/windows-i586/jre-7u2-windows-i586-iftw.exe';
download($url);
... no content is written to the file. And I can't figure out why. The file is created, the call to curl_exec returns true, but the output file remains empty. The file can be downloaded in the browser just fine and the function successfully downloads other files. It's just this file (host?) that I'm having problem with.
Any help is appreciated.
function download($url)
{
$outdir = 'C:/web/www/download/';
// open file for writing in binary mode
if (!file_exists($outdir)) {
if (!mkdir($outdir)) {
echo "Could not create download directory: $outdir.\n";
return false;
}
}
$outfile = $outdir . basename($url);
$fp = fopen($outfile, 'wb');
if ($fp == false) {
echo "Could not open file: $outfile.\n";
return false;
}
// create a new cURL resource
$ch = curl_init();
// The URL to fetch
curl_setopt($ch, CURLOPT_URL, $url);
// The file that the transfer should be written to
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
$header = array(
'Connection: keep-alive',
'User-Agent: Mozilla/5.0',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// downloading...
$downloaded = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
fclose($fp);
if (!$downloaded) {
echo "Download failed: $error\n";
return false;
} else {
echo "File successfully downloaded\n";
return $outfile;
}
}
That url redirects to another. You need to set CURLOPT_FOLLOWLOCATION to 1 for that to work.
Try Adding;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//then after curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
Check some sample: http://www.php.net/manual/en/function.curl-exec.php#84774

Categories