Results 1 to 9 of 9
  1. #1

    Question PHP: Run URL and capture output in a variable

    What PHP function can I use to RUN a URL and then capture the output in a URL??

    I can't use fopen() or any of the file opening functions because a file is not returned, only a short string.

    This short string is displayed in my browser if I request the URL with my browser. I can't find any PHP functions that can read the output of a URL if it is not a file.

    This is such a basic thing to want to do.

    How would you do it??

    Thanks a lot,

  2. #2
    so you just want to get the contents of a url into a string?


  3. #3
    Join Date
    Jul 2003
    Most of the file opening functions have URL wrappers that allow you to open URLs.

    Some depend on php.ini settings (allow_url, if I remember correctly). The safest way to do this sort of things is to use any number of HTTP classes (like snoopy) or cURL if you have that functionality compiled in.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  4. #4
    try to open URL like: $result_string = join('', file(''));
    From Adult Web Hosting Service with love

  5. #5
    the easiest and as far as I know about the only way to do this using a function in PHP is

    $string = file_get_contents("");

  6. #6


    Thanks Flush. For some reason I had over-looked this function but in any case it's no good because the server my site is on is running PHP 4.1.2.

    I can't find anyway to get 4.1.2 to do the job and fear you may be right that file_get_contents() is the only way to do this.

    But because it seems a relatively simple task for PHP to perform, I can't help doubting that it took until version 4.3 to accomodate this.

    Can someone with PHPv4.3+ check that this works:
    PHP Code:
    $urloutput//should print "N,-1,Cart id must be provided" 
    If so, I'll badger my host for a php upgrade.

    Thanks again,

  7. #7
    Join Date
    Jun 2002
    Kansas City

  8. #8
    Can someone with PHPv4.3+ check that this works:

    echo $urloutput; //should print "N,-1,Cart id must be provided"
    I have PHP version 4.3.3 and your script return string:
    N,-1,Could not derive installation ID
    From Adult Web Hosting Service with love

  9. #9

    Smile Solution Found!

    Thanks Adult4Web. That means that function would work.

    Anyhow, for my 4.1.2 version: I've taken the advice of fyrestrtr and BenSummers and used curl (which surprisingly my host had installed). This works:

    PHP Code:
    $ch curl_init();   
    curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable 
    $result curl_exec($ch); 
    Thank you all. This is a pretty class forum...

Posting Permissions

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