Jonatha Suh
Jonatha Suh

Reputation: 195

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?

Upvotes: 11

Views: 1870

Answers (2)

Jason Hendry
Jason Hendry

Reputation: 303

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

echo curl_exec($ch);

Upvotes: 1

Inceddy
Inceddy

Reputation: 760

Because it's not allowed to send cross site requests (this holds for ports as well) you need to do this PHP relais thing.

Never the less. Requesting the same request over and over again (polling) is almost right BUT your web-service should keep the connection open until it has some new information or the request times out (long polling).

What does your web service return (Http-Status ok? Any content?)

Upvotes: 0

Related Questions