TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   cURL (http://www.talkphp.com/advanced-php-programming/1322-curl.html)

WinSrev 10-21-2007 04:38 PM

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=> '' ); 
    } 


sketchMedia 10-22-2007 08:59 AM

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

WinSrev 10-22-2007 09:33 AM

Would that work with the rest of the code though?

sketchMedia 10-22-2007 09:51 PM

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.

Wildhoney 10-22-2007 11:03 PM

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.

bluesaga 10-24-2007 09:55 AM

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.

WinSrev 10-24-2007 04:04 PM

hmm, we'll, i tried it and it just constantly fails. not sure whats wrong.

bluesaga 10-24-2007 04:10 PM

Do you have the cURL extension installed? It needs to be installed to have cURL functionality in your website.

WinSrev 10-24-2007 09:22 PM

i have just about every extension installed and yes, that is one of them

Wildhoney 10-24-2007 10:32 PM

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


sketchMedia 10-25-2007 07:30 PM

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.


All times are GMT. The time now is 06:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0