简体   繁体   中英

Include data from an external site, at runtime in php

I am working on a personal project in PHP where I need to fetch and include data in runtime, or after the page has loaded whichever is easy, and have it inserted in the page markup, as if it was there native.

A simple real life example would be, the Facebook like box, It fetches "data about likes" in realtime, and shows it up on the third party site.

Another example would be voltrank, seo system, where they ask you to just insert a piece of code in the page being rendered in php, and they fill the space you provide with the links from their database. and it gives a feel like the links were there in the first place, and not inserted externally.

And yeah both the sites(the data source + the data destination) are gonna be mine, so no restrictions about content/scraping/or any code implementation whatsoever.

And I don't want it to be iframe based solution, cause I want to pass a value to the source site/url, and want it to return output based on that value.

Furthermore, I am not even asking for the whole solution here itself, All I want is some great prodigy in web programming specially in PHP, guide me in the right direction, cause I cant even figure out what exactly is this process/thing called, which makes it more difficult for me to research on.

Regards (Plz don't backlash me if this question is too naive)

PS: Offcourse The site where I wish to pull the content from has a database system.

And My preference is more inclined towards making it a public system in 'future', where I am to give my clients a specific code snippet(more like facebook does), and then by placing that code on their site, they are able to get their respective data, from my app.

You want php-curl. http://php.net/manual/en/book.curl.php

here is a nice little function

    function curl_download($Url) {
        if (!function_exists('curl_init')) {
            die('Sorry cURL is not installed!');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    $a = curl_download('http://www.yoursite.com/file.txt');
    echo $a;

You should also look into ajax .get(), .load() .jsonp(). You will run into issues using ajax to do cross domain calls, but php doesn't mind. It all depends if you really need the info displayed within PHP's run time, or if you want to quickly load a html page, and then use ajax to grab the data, and fill in the data.

As I know, you need to fetch data from an another site and show it in your site in realtime.

I have done a similar work just a week ago. What i done is,

  • calling the page containing the fetched details using ajax.

  • using settimeout of jQuery refreshing the called page at regular time intervals.

  • user will not know the refresh, since the refreshed page is called by ajax.

Hope it helps.

I think Codeigniter hooks may be your answer. Check out the hook points to see which one best suits your needs:

http://ellislab.com/codeigniter/user-guide/general/hooks.html

You can call a hook on

post_controller

or

post_system

for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM