28, May 2012

copy a file from remote with cUrl - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: copy a file from remote with cUrl
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: copy a file from remote with cUrl  (Read 3517 times)
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« on: Aug 09, 2006, 09:02:02 am »

Hello,

I need to copy a file without using the php function "copy".

I got this example sometime before from Nick:
Code:
<?php
$ch 
curl_init("http://www.bergtoys.info/");
curl_setopt($chCURLOPT_TIMEOUT50);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
$curl_ret curl_exec($ch);

$data curl_getinfo($ch);


curl_close($ch);


where $data is the data I collected via curl, now I need to save this data into a file (image) on the server, do I need to use a fopen function now?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Aug 09, 2006, 09:52:49 am »

Yes, but in the code you posted, the contents of the file are stored in the $curl_ret variable.

The $data has the info about the session (headers, etc)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Aug 09, 2006, 09:57:02 am »

Yes, but in the code you posted, the contents of the file are stored in the $curl_ret variable.

The $data has the info about the session (headers, etc)
OK Wink

do you have an example how to handle error's with curl (since there are no booleans possible like "if (!copy(..."

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Aug 09, 2006, 09:58:27 am »

I check if the strlen($curl_ret) is bigger than 0 and the response header (right now is in the $data)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Aug 09, 2006, 10:05:46 am »

I check if the strlen($curl_ret) is bigger than 0 and the response header (right now is in the $data)

what about this function?
Code:
<?php
function 
copy_with_curl($url) {
$ch curl_init($url);
curl_setopt($chCURLOPT_TIMEOUT50);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
$curl_data curl_exec($ch);
if (curl_errno($ch) == 0) {
if ($fd = @fopen($this->thumbnail_path"wb")) {
fwrite($fd$curl_data);
fclose($fd);
$err true;
} else {
$err true;
}
} else {
$err true;
}
curl_close($ch);
return $err;
}

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Aug 09, 2006, 10:10:44 am »

Nice, but you have a small error Wink

The replacement should be this :

Code:
<?php
function copy_with_curl($url) {
$ch curl_init($url);
curl_setopt($chCURLOPT_TIMEOUT50);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
$curl_data curl_exec($ch);
if (
curl_errno($ch) == 0) {
if (
$fd = @fopen($this->thumbnail_path"wb")) {
fwrite($fd$curl_data);
fclose($fd);
$err false;
} else {
$err true;
}
} else {
$err true;
}
curl_close($ch);
return 
$err;
}

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Aug 09, 2006, 10:12:16 am »

yes thats right twice a true, thanks

is this curl a standard on linux shared hostings?

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #7 on: Aug 09, 2006, 10:14:50 am »

yes thats right twice a true, thanks

is this curl a standard on linux shared hostings?

I am not sure about this, but you can check it :

Code:
<?php
if ( !extension_loaded('curl') )
     echo 
'Problem....';

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Aug 09, 2006, 10:40:31 am »



I am not sure about this, but you can check it :

Code:
<?php
if ( !extension_loaded('curl') )
     echo 
'Problem....';


is there only a curl extension?
(then I'm able to check this via phpinfo())
« Last Edit: Aug 09, 2006, 11:04:59 am by olaf »

Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Aug 09, 2006, 11:38:01 am »

yes, curl is available on all shared hosts I'm using (6 diff. configurations)

and I learned that there are much more configuration settings then with functions like fopen... COOL!

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #10 on: Aug 09, 2006, 11:54:25 am »

and I learned that there are much more configuration settings then with functions like fopen... COOL!

Of course. Actually curl is an 'interface' to fopen, and it uses fopen to work....

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Aug 09, 2006, 12:29:25 pm »

and I learned that there are much more configuration settings then with functions like fopen... COOL!

Of course. Actually curl is an 'interface' to fopen, and it uses fopen to work....

strange is that the setting "allow_url_fopen" is not blokking here...

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #12 on: Aug 09, 2006, 12:34:00 pm »

This is propably because curl is using fopen from the Zend engine level (php.ini restrictions are not a problem there)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Aug 09, 2006, 12:38:27 pm »

This is propably because curl is using fopen from the Zend engine level (php.ini restrictions are not a problem there)
Yes that's possible...

Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3588
Tags : php curl zend linux Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: copy a file from remote with cUrl
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 02:54:35 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.