Results 1 to 7 of 7

Thread: curl_init()

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    curl_init()

    Hi, I have this piece of code that sends a request for a url
    Code:
    } 
     
    // Make an http request to the specified URL and return the result 
     
    function make_http_request($url){ 
          $ch = curl_init($url); 
     
          curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     
          $result = curl_exec($ch); 
     
          curl_close($ch); 
          return $result; 
    }
    But for one reason or another curl_init doesnt work. how do i convert this to another method like fopen or something, i have fidled with it but cant get it to work
    please can someone have a look at this.
    thanks

    alex

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: curl_init()

    file_get_contents($url) will work if you have URL wrappers on.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: curl_init()

    Sorry, I'm very new with requests, i'm doing something my boss asked me to do - alexa.com the web stats site, allow you to download lists of top sites from countries, but its really weird you have to edit this php they give you, send the request and it returns the list, but the curl_init() isnt working, and i researched a bit, and apparently i can use a diff method. How do i turn url wrappers on, and can i just put
    Code:
    file_get_contents($url)
    in place of
    Code:
    curl_init($url);
    . Do i need something else, or thats fine i.e can the end code just be
    Code:
    } 
     
    // Make an http request to the specified URL and return the result 
     
    function make_http_request($url){ 
          $ch = file_get_contents($url)
     
          curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     
          $result = curl_exec($ch); 
     
          curl_close($ch); 
          return $result; 
    }
    I know in these forums, I am supposed to do it myself, but I am having real trouble with this bit, and all help is much appreciated

    Thanks

    alex

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: curl_init()

    If you are unsure what a function does then the first thing to do is whizz over to the PHP.net manual.
    PHP: file_get_contents - Manual

    You should be able to replace the entirety of your code with that single call, which will return a string on success, or FALSE on failure. Test that like this:
    PHP Code:
    $data file_get_contents($url);
    if (
    $data !== false) {
      
    // success code here
    }
    else
      
    // failure code here


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: curl_init()

    Thanks for all your help penegate, But I am now getting errors about the $result. It = an http request.
    atal error: Call to undefined function: make_http_request() in /ho.....
    I have attached the whole code hopefully so you/someone can have a look, the bit about the curl_init, now replaced with file_get contents is right at the bottom...

    Thanks
    Alex
    Attached Files Attached Files

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: curl_init()

    Look for where make_http_request() is being called: on line 30. It's not defined above and the code below it expects that the request has been made, so the file_get_contents call is in the wrong place.

    You need to replace this:
    PHP Code:
    $result make_http_request($ats_url); 
    with this:
    PHP Code:
    $result file_get_contents($ats_url); 

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: curl_init()

    Quote Originally Posted by youngnoviceinneedofh
    Thanks for all your help penegate, But I am now getting errors about the $result. It = an http request.

    I have attached the whole code hopefully so you/someone can have a look, the bit about the curl_init, now replaced with file_get contents is right at the bottom...

    Thanks
    Alex
    Tell your boss he needs to employ someone capable of thinking for themselves.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width