Curl or get_file_contents() ?

Discussion in 'PHP' started by adbox, Jan 15, 2010.

  1. #1
    I need to run a an external script by opening it, using curl or some like, but I don't want to wait on it to load, or need to have it's contents. I just want to ping it and have it run itself leaving my operator script free to go on.

    any advice? Is CURL my best option? Is there something else?
     
    adbox, Jan 15, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you can do it literally by just opening and closing the connection to it
    $h = fopen('http://www.yoursite.com/file.php', 'r');
    fclose($h);
    PHP:
     
    JAY6390, Jan 15, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi adbox,

    You can use img html tag for this (when you don't need any responce nor waiting for it):
    
    <img src="http://www.yoursite.com/file.php" height="0" width="0" alt="" />
    
    Code (markup):
    ^^This line can be echo-ed in php or placed in plain html.
    Regards,
    Nick
     
    koko5, Jan 15, 2010 IP
  4. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #4
    If you have to do it from server itself, then CURL is best. Fork a background process and its done.
    If you have to do it from client, method suggested by koko5 makes sense and is used widely.
     
    mastermunj, Jan 17, 2010 IP
  5. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #5
    but does curl attempt to load the contents? I would like 0 wait time if possible, even if the script takes 30 seconds to run.

    Right now I'm using jays method, any comment on that?
     
    adbox, Jan 18, 2010 IP
  6. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I use file_get_contents() for simple tasks, like getting a specific word or sentence from a dynamic page.
    I use CURL for more complicated tasks (logins, POSTing binary data and so on), CURL is also faster than anything else.
    You could launch the script and stop it with Javascript (the script will keep on running on server if ignore_user_abort(true); is set)
     
    iAreCow, Jan 18, 2010 IP
  7. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #7
    CURL is the best option where you want to run process in background and also need multiple threads..

    In fact, if you have CURL, I believe you won't need anything else..
     
    mastermunj, Jan 18, 2010 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Then you need to do the fetch in the background. Depending on what you want to do with the results, the easiest way is to just do it in an iframe. That way the rest of the page will draw, and the curl-dependent content will fill in when it's available.

    Otherwise, if you want to be fancier, you can use the curl_multi functions to perform a transfer in the background while your program gets on with other things. Then, you can flush the output buffer and wait around for the curl operation to complete if it hasn't already. If you've never dealt with async calls this might initially be a frustrating journey but once you get your head around it you'll be more powerful than ever.
     
    SmallPotatoes, Jan 20, 2010 IP
  9. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #9
    As of late I've been using Jay's method. Anybody believe this is a bad approach?
     
    adbox, Jan 20, 2010 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    Kaizoku, Jan 21, 2010 IP
  11. skyrackson

    skyrackson Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    what is the use of curl? can anybody explain it?? whether is curl is extension of PHP or some other language??
     
    skyrackson, Jan 23, 2010 IP
  12. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    insert, Jan 24, 2010 IP
  13. skyrackson

    skyrackson Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    ya thanks for your idea insert.
     
    skyrackson, Jan 24, 2010 IP
  14. hasanbasri

    hasanbasri Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    what the meaning of line ? :confused:
     
    hasanbasri, Jan 24, 2010 IP
  15. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #15
    iAreCow, Jan 24, 2010 IP
  16. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #16
    You don't want to display the file you just want to run it. Simple enough answer is to set a CRON job, or a Scheduled Task. Any decent host should offer this.
     
    FCM, Jan 24, 2010 IP
  17. Bananasphere

    Bananasphere Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    file_get_contents() is to simply get the literal source code (html) from a webpage.

    Curl is not only faster, but it allows for more options. You can send POST values using curl among other things.

    If you just want to run it, file_get_contents or fopen() aren't bad choices, just don't echo the contents if you don't want to see them.
     
    Bananasphere, Jan 24, 2010 IP