TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-21-2007, 04:38 PM   #1 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
cURL

Hey,

Was just wondering if this little bit of code could be converted to a cURL function instead of File_get_contents, a little but stumped about how to do that with this code, was wondering if anyone could do it? thanks.

PHP Code:
    function file_get_content$url )
    {
        for( 
$i 1$c <= $this->conf['tries']; $c++ )
        {
            
$file = @file_get_contents$url );
            if( 
$file != FALSE )
            {
                return array( 
=> TRUE=> $file );
                break;
            }
        }
        return array( 
=> FALSE=> '' ); 
    } 
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 10-22-2007, 08:59 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

This is how to use cURL to grab a file
PHP Code:
/*First we Initialize a new cURL session and pass in our url,
then it returns a cURL handle */
$ch curl_init("http://www.blahblah.com/file.txt");

/*We must set CURLOPT_RETURNTRANSFER to true otherwise it just farts
 it out to screen instead of putting it into $file when we run curl_exec()*/
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

//Execute and put contains in $file
$file curl_exec($ch);

//close the cURL session
curl_close($ch); 
Hope that helps
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 10-22-2007 at 09:27 AM. Reason: spelling error
sketchMedia is offline  
Reply With Quote
Old 10-22-2007, 09:33 AM   #3 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

Would that work with the rest of the code though?
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 10-22-2007, 09:51 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

should do:
PHP Code:
function file_get_content$url )
{
    for( 
$i 1$c <= $this->conf['tries']; $c++ )
    {
        
$ch curl_init($url);

        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
        
$file curl_exec($ch);//Returns file contents string, FALSE on failure
        
        
curl_close($ch);

        if(
$file)
        {
            return array( 
=> TRUE=> $file );
            break;
        }
    }
    return array( 
=> FALSE=> '' );

I havent tested it, but theoreticly it should work.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-22-2007, 11:03 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Why do you want to use cURL anyway? I think Salathe pointed out a good way to get the HTTP headers in an earlier thread of yours.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-24-2007, 09:55 AM   #6 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

I would recommend doing as above, but put the curl call into a function and name it like file_get_contents_curl or something, easier to understand then and more portable.
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 04:04 PM   #7 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

hmm, we'll, i tried it and it just constantly fails. not sure whats wrong.
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 10-24-2007, 04:10 PM   #8 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Do you have the cURL extension installed? It needs to be installed to have cURL functionality in your website.
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 09:22 PM   #9 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
Default

i have just about every extension installed and yes, that is one of them
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 10-24-2007, 10:32 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I've modified it only slightly, but this works perfectly for me:

PHP Code:
function file_get_content$url )
{
    for(
$i 1$i <= 3$i++)
    {
        
$ch curl_init($url);

        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        
        
$file curl_exec($ch);//Returns file contents string, FALSE on failure
        
        
curl_close($ch);

        if(
$file)
        {
            return array( 
=> TRUE=> $file );
            break;
        }
    }
    
    return array( 
=> FALSE=> '' );
}  

var_dump(file_get_content('http://www.wiredflame.com/')); 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-25-2007, 07:30 PM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
i tried it and it just constantly fails
what exactly do you mean, does it produce an error? also ive just noticed somthing a bit 'iffy' in your for loop (didnt actually check it through when i looked last, i just replaced file_get_content() with cURL)
PHP Code:
 for( $i 1$c <= $this->conf['tries']; $c++ ) 
should that not read:
PHP Code:
 for( $i 1$i <= $this->conf['tries']; $i++ ) 
dunno if that was your problem, i dunno what the rest of your script looks like so i might be off the mark so to speak.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:10 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design