I found this code
(full version here http://blog.loicg.net/developpement-web/lire-twitter-stream-php-curl)
function read_the_stream($sTrackingList){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://stream.twitter.com/1/statuses/filter.json');
curl_setopt($ch,CURLOPT_USERPWD,TWITTER_LOGIN.':'.TWITTER_PASSWORD);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Twitter-Client: ItsMe','X-Twitter-Client-Version: 0.1','X-Twitter-Client-URL: http://rien.net/'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"track=".$sTrackingList);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');
curl_exec($ch);
curl_close($ch);
}
It's work for me but now i don't know how to stop it (for making test it's horrible, my new php file not interpreted!)
You should either put this code inside a script that is not executed on a webpage (like a daemon) or you should not use the stream.
If you want to get only 100 tweets, then use some of the other twitter API for that.
If you want a stop button, consider:
have your other script populate some database (or a queue like redis or ActiveMQ) and then you can have your webpage get your own stream using AJAX
Simply using AJAX to call a script that reads the feeds.
Related
I'm using cURL php to display content, I know cURL can be resourceful and obviously increase loading time so I'm trying to implement a cache logic system to read HTML content from a file and not using cURL.
Now my question is how to determine if a page has been updated compared to cache version between 2 visits without doing a cURL request?
My cURL function at the moment is as follow:
function getHTML($url,$timeout)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
return #curl_exec($ch);
}
Your ideas and suggestions are more than welcome! Thanks a lot in advance!
I am calling three methods which executes xml using curl
$clientID=$newInvoice->reateClient($name,$organization,$email,$address,$country);
$invoiceID=$newInvoice->CreateInvoice($clientID,$invoiceNumber,$itemName,$cost,$quantity);
echo $newInvoice->SendInvoice($invoiceID);
when I use these all three method then first executes properly but second and third shows error that : String could not be parsed as XML
Its is working fine if I call each method one at a time by commenting other two method. Can anyone please help
php code sample($xml_request contains xml and it seems ok because I can execute without any error if I execute one method at a time by commenting any other two ):
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_USERPWD, $token.':123');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml_request='.$xml_request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Invoicera API Test 1.0");
$curl_result = curl_exec($ch);
curl_close ($ch);
return $curl_result;
I am running a script which uploads a file to remote form
//submit form
$form_data = array("file_upload" => "#file.xml;type=text/xml","otherkey" => "overvalue");
$ch = curl_init();
$form_url = 'http://www.domain.com/form.php';
curl_setopt($ch, CURLOPT_URL, $form_url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$postResult = curl_exec($ch);
echo $postResult;
The file is being uploaded perfectly, however on the remote server, after file upload and processing the page is reloaded (form.php), however the CURL post simply begins again.
I am guessing this is due to the POST data still being present? how can I do this so that the file is uploaded once, processed and then completed? I have set FOLLOWLOCATION = 0 but with no luck
Many thanks,
How do you know it begins again? If you infer it from your echo, it is echoed twice as you write this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //echoes result at init
instead of this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //returns only result, not echoing
I have another question concerning YouTube upload. Based on the answer that I received from khael here (replace form input file with direct file upload), the script uploads the video perfectly. However, the curl response is "Moved Temporarily". What I need instead is to get the YouTube video ID back.
Another problem is that the "nexturl" never gets called. If it was called, I could easily read the video id with "$_GET['id']" and write it to the database.
Here the code I'm working with
$ch = curl_init($response->url."?nexturl=".urlencode($nexturl));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file"=>"#/path/to/file.avi",
"token"=>$response->token
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
Thanks,
Florian
Without testing I'd guess that if you add the FOLLOWLOCATION option you'd get what you're after:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
I need to check if remote file are online and they didn't change. The problem is that those files are big, so I want to read the http header only and then abort the request. The result would be based on response status code and content length field.
How can I do it in PHP?
You can use the get_headers() function.
If you prefer cURL, you can use CURLOPT_NOBODY:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers = curl_exec($ch);