2

I wonder if there's a way to use curl trhough an already open socket, something like, adapting this:

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

to use curl_exec() instead of fgets($fp, 128)

(or, any other way to use curl() over the same stream all the time, my goal is to read the twitter stream api)

Thank you

w00
  • 26,172
  • 30
  • 101
  • 147
K. Weber
  • 2,643
  • 5
  • 45
  • 77
  • Except you want to combine curl and `fgets` together – Baba Oct 16 '12 at 11:41
  • No, cURL has its own internal socket handling. What is the real issue you are facing? Why do you want to initiate a socket for cURL while cURL can do that itself pretty good? What problem are you trying to solve? – CodeCaster Oct 16 '12 at 11:53
  • Twitter stream api forces clients to use a single connection and stay there listening "forever", so I need curl() to use the same connection always rather than connect/read/disconnect. Also, I want curl instead of fwrite($fp, $out) as I use a class that uses curl and OAuth, which I need for Twitter API – K. Weber Oct 16 '12 at 12:01
  • [cURL by default reuses its connections](http://stackoverflow.com/questions/972925/persistent-keepalive-http-with-the-php-curl-library). – CodeCaster Oct 16 '12 at 12:20
  • I can give example if you want – Baba Oct 16 '12 at 12:25
  • I'll give it a try, then. I have to leave this task till thursday so... if your example is already written it's always welcome, if not... then as you wish, I will go on with this in 2 days. Thanks! – K. Weber Oct 16 '12 at 12:31

2 Answers2

2

This might work for you since you are dealing with Twitter Stream API

set_time_limit(0);
$ch = curl_init();
echo "<pre>";
curl_setopt($ch, CURLOPT_URL, "https://sitestream.twitter.com/2b/site.json");
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'cgets');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000000);
curl_exec($ch);

function cgets($ch, $string) {
    $length = strlen($string);
    printf("Received %d byte\n", $length);
    flush();
    return $length;
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • But I'm using version 1.1 of the api so I need OAuth, plus, there should be an _infinite_ loop somwhere – K. Weber Oct 16 '12 at 21:17
0

There is no way to force cURL to use an existing socket in PHP. While cURL pools and reuses the connections internally, it cannot be forced to use the same one all the time, or never close a connection it opened.

You can checkout Phirehose (which is a PHP interface to the streaming APIs) or if that's not good enough, do some shopping on the Twitter Libraries list - chances are somebody already solved your problem in PHP and packaged it.

Still, using the stream API sound strange from PHP. Is this on a web server, or a stand-alone long-running script?

Rick Kuipers
  • 6,616
  • 2
  • 17
  • 37
Jakub Wasilewski
  • 2,916
  • 22
  • 27