I retrieve a Blob with curl :
$ch = curl_init("http://MYURLFORRETRIEVEANIMAGE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$img = curl_exec($ch);
curl_close($ch);
So $img is my BLOB.
Now, i want to upload this blob on my ftp..And i don't know exactly what i should do ??
Thanks :)
PHP has an FTP extension. Using this, you can take the file, and upload it to your FTP server.
Is your ftp located on the same machine as your script? If yes: just write the content of $img via fwrite
Related
How to connect to the exchange 2013 EWS just get photo ?
What Library do I need (API) and how to embed it ? (I 'm beginner in php)
Here's a code I have now:
https://exchange.domen.local/ews/exchange.asmx/s/GetUserPhoto?email=mail#mail.ru&size=HR240x240
He ask me login/password. That's good. But I need a way to write the Login/password to the script. Thanks.
Here is how I handled this in PHP using curl. It is simple with no dependencies outside of curl.
Tested against an Exchange 2013 server. Saves to a file directly.
$server = ''; // owa.whatever.com, etc.
$user = ''; // username without domain info
$password = '';
$email_to_get = ''; // Email to pull photo
$fullurl = "https://$server/ews/Exchange.asmx/s/GetUserPhoto?email=$email_to_get&size=HR648x648"; //sizes defined at https://msdn.microsoft.com/en-us/library/jj194329(v=exchg.80).aspx
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullurl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM | CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$returned = curl_exec($ch);
$fp = fopen("pic.jpg", 'w'); // Save picture locally to .jpg
fwrite($fp, $returned);
fclose($fp);
header('Content-type: image/jpeg');
echo $returned; // Display the image on the page if desired
Your "Code" you provide is incomplete. You only trigger the URL but do not specify that you wish to fetch the picture from the xml stream.
The best way is to check the Microsoft HowTo here, it provides examples you could adjust to your needs. If you are unable to do that you might wish to check the PHP EWS library from here.
The $source is the url link of an image from a website that requires authentication/login. I was able to download the image but the downloaded image can't be opened. I knew the image is not downloaded properly as it is asking for credentials. I use CURL to download the image and now, I have no idea what's wrong with the code, what else should i add to it??
<?php
$username = $_SESSION["username"];
$password = $_SESSION["password"];
$source = 'https://example.com/sequence.png';
$fp = fopen('C:/image/'.basename($source), 'w');
$ch = curl_init($source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");// to use for connection
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 1);// content type
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_exec($ch);
fclose($fp);
?>
If your authentication is not basic, you need some library like Guzzle.
You can do the login process and then download the image
I was trying to upload image using curl put request, And was able to upload image if the image is stored in local directory. but the problem is that i have to upload image from another image url.
could anyone help me.
$localfile = "testing.jpg";
// echo filesize($localfile); die;
$url = API_URL . "pages/343/files/=".$localfile."?description=testing";
$fp = fopen ($localfile, "r");
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
// $jsonOutput = json_decode($http_result, true);
curl_close($ch);
fclose($fp);
i have tried to change $localfile to
$localfile = "https://someurl/someimage.png";
but it gave me following error.
Warning: curl_setopt(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE* in C:\xampp\htdocs\newPutMedia.php on line 29
Warning: filesize(): stat failed for https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png in C:\xampp\htdocs\newPutMedia.php on line 30
Apparently this is a bug in PHP cURL. You can use the bug fix as mentioned here or you can do this :
Copy the file from URL to your local directory :
file_put_contents(__DIR_\_ . "/testing.jpg", file_get_contents($url));
Use the new file stored in your server for cURL PUT request :
$localfile = __DIR_\_ . "/testing.jpg";
Since the bug does not exist for file reading from your server, the bug won't cause errors and this would perfectly work.
I have images that are hosted on a CDN, and I want to upload that image to some other system via their API. Normally I would just use curl and put an # character in front of the file to upload. Since this is a URL, that won't work since it expects the file to be local. I don't want to copy it local first since the files could be videos and that could be time consuming.
It seems there should be a way to do this with PHP curl. I'm open to using sockets instead, but I'm not sure how to simulate how curl does the submission.
<?php
$save_file_url = file_get_contents($your_files_CDN_file_URL);
$fp = fopen('test','w');
fwrite($fp,$save_file_url);
$save_file = realpath('test') ;
$url = "http://URL_TO_SEND_FILE/get_file.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file_is_this"=> "#".$save_file,
"app"=>'test'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
And you will recieve file at get_file.php.
I am using the following code to transfer an image and it is working except the jpg is corrupted after the transfer. Is says invalid image format and shows a blurred image.
I tried using regular php without curl and get the same results.
Does anyone know why whatever I try works but corrupts the image.jpg
$curl = curl_init();
$fh = fopen("test.jpg", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://{$serverInfo['user']}: {$servererInfo['password']}#{$serverInfo['ftp1.server.com']}/{$serverInfo['For_Web/Web Images/Full Size/00-99/file']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
fwrite($fh, $result);
fclose($fh);
curl_close($curl);`
There are a few problems;
You should open your file for writing in binary mode, that is;
$fh = fopen("test.jpg", 'wb');
curl_exec returns a bool (success), not the contents of the file, the file should instead be passed to CURLOPT_FILE.
You should set the username/password using CURLOPT_USERPWD, not sure if the URL way could be made to work too, though.
You should set CURLOPT_BINARYTRANSFER.
Working sample;
$curl = curl_init();
$fh = fopen("fips.exe", 'wb');
curl_setopt($curl, CURLOPT_URL, 'ftp://ftp.sunet.se/pub/FreeBSD/tools/fips.exe');
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_USERPWD, 'anonymous:olle');
$result = curl_exec($curl);
fclose($fh);
curl_close($curl);