SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,734
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)

    Include HTML OUTPUT of another page into a PHP variable?

    How can I set the value of a PHP variable to be the HTML OUTPUT of another page into a PHP variable?

    For example I can get TWEETS directly as a JSON object from Twitter by going to a user's URL. But rater than use AJAX, I would like to just include the data directly into my page as it's being built by my PHP script. This would be a piece of cake if I could get the output of said URL directly into a PHP variable,in other words:

    $JSONobj= whatever URL returns;
    $tweets= (json_decode($JSONobj);

    ...I there a way to do this or something like it directly in PHP?

    All help greatly appreciated!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Maybe with curl?

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,734
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    Is there any other method? I am not transferring a file, or replacing HTML.. am merely getting a stringified object

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is the targeted "other page" actually a file on the same server then?

  5. #5
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $json file_get_contents('http://www.site.com/path/to/json/feed'); 
    Don't forget to add http(s):// so that file_get_contents knows you're pulling a webpage's source.

  6. #6
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,734
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    That's the function i was trying to remember!!! Thanks Kduv.

    cURL .. also worked,as guido suggested, but ,for this, it seemed like overkill.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dresden_phoenix View Post
    That's the function i was trying to remember!!! Thanks Kduv.

    cURL .. also worked,as guido suggested, but ,for this, it seemed like overkill.
    Maybe, but cURL always works, and file_get_contents can be locked down to not get external content on some web servers. So it is always good to recommend either
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I found simplexml_load_file the other day after a search. It's also another way. Use libxml_use_internal_errors() to supress the errror (although worth looking at. Saves going to w3c validator!)
    If you're used to a bit of XML it's quite useful.

    http://www.php.net/manual/en/functio...-load-file.php
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Bookmarks

Posting Permissions

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