Skip to content

Commit d62a611

Browse files
committed
Merge pull request Fotolia#3 from Manackkel/master
Add downloadMediaComp method
2 parents 8ea09e7 + 27a0e69 commit d62a611

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

php/example.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
}
2323
}
2424

25+
// download comp file
26+
$comp_dl_data = $api->getMediaComp(35957426);
27+
$api->downloadMediaComp($comp_dl_data['url'], '/tmp/comp.jpg');
28+
2529
// loggin in and retrieving user data
2630
$api->loginUser('your_login', 'your_password');
2731
print_r($api->getUserData());

php/fotolia-api.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,29 @@ public function getMedia($id, $license_name, $subaccount_id = NULL)
314314
* @param string $output_file if null the downloaded file will be echoed on standard output
315315
*/
316316
public function downloadMedia($download_url, $output_file = NULL)
317+
{
318+
return $this->_download($download_url, $output_file);
319+
}
320+
321+
/**
322+
* Download a media comp and write it to a file if necessary
323+
*
324+
* @param string $download_url URL as returned by getMediaComp()
325+
* @param string $output_file if null the downloaded file will be echoed on standard output
326+
*/
327+
public function downloadMediaComp($download_url, $output_file = NULL)
328+
{
329+
return $this->_download($download_url, $output_file, false);
330+
}
331+
332+
/**
333+
* Download a content and write it to a file if necessary
334+
*
335+
* @param string $download_url URL
336+
* @param string $output_file if null the downloaded file will be echoed on standard output
337+
* @param bool $http_auth set curl password if needed
338+
*/
339+
private function _download($download_url, $output_file = NULL, $http_auth = true)
317340
{
318341
$ch = $this->_getCurlHandler($download_url);
319342

@@ -327,7 +350,9 @@ public function downloadMedia($download_url, $output_file = NULL)
327350
}
328351

329352
curl_setopt($ch, CURLOPT_FILE, $output_fd);
330-
curl_setopt($ch, CURLOPT_USERPWD, $this->_getHttpAuth(TRUE, TRUE));
353+
if ($http_auth) {
354+
curl_setopt($ch, CURLOPT_USERPWD, $this->_getHttpAuth(TRUE, TRUE));
355+
}
331356

332357
$response = curl_exec($ch);
333358

0 commit comments

Comments
 (0)