Daniel Fein
Daniel Fein

Reputation: 327

How do I call an API without reloading the page with PHP?

I am looking to call the Twitter API to grab tweets (successfully achieved tweets on load) but now I am looking to update the page automatically, allowing the tweets to automatically load without reload/user interaction.

I know this type of functionality is possible (monitter.com) does it, but what is the technology used to do so? Can I do it with PHP?

Thanks

Upvotes: 0

Views: 334

Answers (2)

rohitarora
rohitarora

Reputation: 1365

use curl

function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")

            {
                if($login == 'true') {
                    $fp = fopen("cookie.txt", "w");
                    fclose($fp);
                }
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
                curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

                curl_setopt($ch, CURLOPT_TIMEOUT, 60);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                if ($proxystatus == 'true') {
                    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
                    curl_setopt($ch, CURLOPT_PROXY, $proxy);
                }
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

                if($secure=='true')
                {
                    curl_setopt($ch, CURLOPT_SSLVERSION,3);
                }

                curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );


                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_REFERER, $ref_url);
                curl_setopt($ch, CURLOPT_HEADER, TRUE);
                curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                curl_setopt($ch, CURLOPT_POST, TRUE);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                ob_start();

                return curl_exec ($ch); // execute the curl command

                curl_getinfo($ch);
                ob_end_clean();
                curl_close ($ch);
                unset($ch);
            }

just call this function the way you want the data you want to set it can do every thing for you just dont forget to setup curl in you php.ini

thanks

Upvotes: 0

Rowinson Gallego
Rowinson Gallego

Reputation: 856

As @suresh.g said, you can use AJAX. The simplest way: use jQuery.

Also, you can use an iframe that reloads every 10 seconds with the setInterval() javascript function. The user will not have a reload of his entire page, but the twitter iframe.

Another type of technology is COMET or PUSH technology, but I don't think you need it right now, but it's good to know about it ;)

Upvotes: 1

Related Questions