Download image from website that requires authentication/credentials using php - php

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

Related

Slack. How to download image in url_private property using PHP?

I am trying to download a png image from Slack and save it into a file but the only thing that gets saved into the file is HTML code for the login page of Slack.
This is what I have tried.
$url = 'https://files.slack.com/files-pri/XXXXXX-XXXXXXX/download/2016-07-11.png';
$ch = curl_init($url);
$fp = fopen('/assets/tmp/1.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer xoxp-XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXXXXXXX'
));
curl_exec($ch);
curl_close($ch);
fclose($fp);
If I access the link in browser while logged into Slack, the image downloads just fine, but not through the PHP code. Any help would be appreciated.
I needed an extra permission scope to read files. I added files:read permission scope to my application from Slack Developer Console and the code works as expected.

Upload a URL as a file using PHP curl

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.

How can i upload a blob on my FTP

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

Image corrupted after php curl transfer FTP

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

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

Categories