Reply
Old 06-02-2008, 06:55 PM cURL help
Average Talker

Posts: 23
I need a little help with a script i am writing. My current server disables allow_url_fopen so I can't copy files from URLs, so I am forced to use cURL, which i hardly have a clue how to use.

Here's what I have:
PHP Code:
function get_file($file)
{
    
$ch curl_init();
    
curl_setopt($chCURLOPT_URL"http://www.mysite.com/images/");
    
curl_setopt($chCURLOPT_POST);
    
curl_setopt($chCURLOPT_POSTFIELDS,"@" $file);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
$postResult curl_exec($ch);

    if (
curl_errno($ch))
    {
        print 
curl_error($ch);
    }
    
curl_close($ch);

and:
PHP Code:
if(!get_file($newURL))
            {
                print(
"Not copied");
            } 
I always get my error message "Not Copied". Please help me get it to work!
Thanks, bytes
64bytes is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-02-2008, 07:05 PM Re: cURL help
tripy's Avatar
Fetchez la vache!

Posts: 1,989
Name: Thierry
Location: In the void
Nope, don't gonna work that way.
First, your function don't return anything, so get_file() always see it like a FALSE being returned.

Second, the CURLOPT_POST, 1 line in your function says you want to post datas to the url you specified, but you don't give any data.
If you want to get data, you don't do a CURLOPT_POST.

This is something I use actually in my fetchall_library:
PHP Code:
function getPage($url="http://www.example.com/"){
    
$ch curl_init();
    
curl_setopt($ch,CURLOPT_URL$url);
    
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    
curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/');
    
curl_setopt($ch,CURLOPT_TIMEOUT,30);
    
$html=curl_exec($ch);
    
curl_close($ch);
    return 
$html;

I use it to fetch a web page HTML content.
Simply point it to a file, and the function will return you the content of that downloaded file.
Save it anywhere, and you'll be set.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 06-02-2008, 07:33 PM Re: cURL help
Average Talker

Posts: 23
So if my goal is to copy it to that directory;
Quote:
http://www.mysite.com/images/
PHP Code:
function get_file($url)
{
    
$ch curl_init();

    
curl_setopt($ch,CURLOPT_URL$url);
    
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,15);
    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    
curl_setopt($ch,CURLOPT_REFERER,'http://www.google.ch/');
    
curl_setopt($ch,CURLOPT_TIMEOUT,30);

    
$html curl_exec($ch);

    
curl_close($ch);

    return 
$html;
}  

function 
post_file($file)
{
    
$ch curl_init();

    
curl_setopt($chCURLOPT_URL"http://www.mysite.com/images/");
    
curl_setopt($chCURLOPT_POST);
    
curl_setopt($chCURLOPT_POSTFIELDS,$file);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);

    
$result curl_exec($ch);

    if(
curl_errno($ch))
    {
        return 
false;
    }
    else
    {
        return 
true;
    }

    
curl_close($ch);

}

$myfile "http://www.google.com/intl/en_ALL/images/logo.gif";

if(!
post_file(get_file($myfile)))
{
     print 
"Error";

...I could do that?

Last edited by 64bytes : 06-02-2008 at 08:11 PM.
64bytes is offline
Reply With Quote
View Public Profile
 
Old 06-02-2008, 08:01 PM Re: cURL help
tripy's Avatar
Fetchez la vache!

Posts: 1,989
Name: Thierry
Location: In the void
not exactly.
First, the postfield is a list of key + values, like a get request.
Code:
key=val&key2=val2
Second, it means that you have to read the image datas first, and to put them into the string. But I haven't done anything like that before, and I suppose that the special characters you might encounter in binary files can get in your way.

Maybe you need to escape them, or to convert the file data to base64, I don't know...

Third, your function still don't return anything, so why do you keep having a if() statement looking for it's return value?
Either remove the if(), or make the post_file() function return TRUE when the if should be true, and FALSE otherwise.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 06-02-2008, 08:27 PM Re: cURL help
Average Talker

Posts: 23
maybe I will just take the personal php.ini approach

Last edited by 64bytes : 06-02-2008 at 08:55 PM.
64bytes is offline
Reply With Quote
View Public Profile
 
Old 06-02-2008, 09:28 PM Re: cURL help
scutari's Avatar
Skilled Talker

Posts: 95
Name: Indrit
is get page a function you created especially for doing this?
What does this function do?
__________________
Publishers
scutari is offline
Reply With Quote
View Public Profile
 
Old 06-03-2008, 03:40 AM Re: cURL help
tripy's Avatar
Fetchez la vache!

Posts: 1,989
Name: Thierry
Location: In the void
as I said:
Quote:
I use it to fetch a web page HTML content.
This is what I use getPage() for.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to cURL help
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13996 seconds with 13 queries