Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long polling with php curl

I am following this example for Spring MVC chat client which used HTTP long polling.

My web server is located at port 7555, and I need to be able to make an HTTP long polling request to port 7555 from port 80 (browser) so I created a PHP script that calls my webservice.

<?php
$index = $_GET["index"];
echo $index;
echo $index2;

$urlVar = "http://localhost:7555/test?" . $index . $index2;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlVar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT, 7305);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_exec($ch)
?>

I call this PHP file from my JavaScript with parameters like this:

($.ajax({
    url : "http://localhost/myphpscript.php?index=" + i, 
    type : "GET", 
    cache: false,
    success : function(messages) {
       //do stuff
    }
}));

The PHP file is located is located in my localhost. This does not seem to work because the JavaScript seems to calling the PHP (which calls the URL) endlessly. Am I doing long polling correctly with PHP curl? Do I need to make the Ajax call in JavaScript since I am the HTTP call in curl?

like image 964
Jonatha Suh Avatar asked Jul 13 '15 19:07

Jonatha Suh


1 Answers

With CURLOPT_RETURNTRANSFER you'll need to echo the results of curl_exec($ch)

echo curl_exec($ch);
like image 97
Jason Hendry Avatar answered Oct 02 '22 13:10

Jason Hendry