通过AJAX将动态参数从JavaScript传递到PHP,以便在cURL调用中使用

I am using php curl to make a http get long polling request from my javascript using Ajax. Here is the call from javascript

var i;
i++;
$.ajax({
   url:"http://localhost/myport.php",
   type: GET,
   success: function(response){ ...},
   ...
   ...

Here is how I make the php call in the myport.php file

 <?php
 $ch=curl_init();
 $curl_setopt($ch, CURLOPT_URL, "http://localhost:7555/test?index=" //Here I need to set a value (the variable i) in the above JS

If I making the call directly from js, I would do

$.ajax({ url:"http://localhost:7555/test?index=" + i

I am new to php and curl, I am wondering how I can pass the value of that variable so I can get a parameter for the call.

If I understand correctly, and you just want to append the value of the variable $i to the cURL call, you'd do this:

<?php
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:7555/test?index=" . $i);

Or even,

curl_setopt($ch, CURLOPT_URL, sprintf("http://localhost:7555/test?index=%d", $i));

Also note hat there is no $ before a function call: it's curl_setopt() not $curl_setopt() ($ is for variables, like $ch).

Edit

Upon clarification of the question, it seems you need to get this i variable from JavaScript to PHP. You can pass it as a GET parameter in your AJAX call:

var i;
i++;
$.ajax({
   url:"http://localhost/myport.php?index=" + i,
   type: GET,
   success: function(response){ ...},
   ...
   ...

Then, from PHP, you can use it like this:

curl_setopt($ch, CURLOPT_URL, sprintf("http://localhost:7555/test?index=%d", $_GET['index']));

You should also verify that $_GET['index'] was actually passed in:

if (!isset($_GET['index']))
{
    die("The index was not specified!");
}

You can pass i from JavaScript like in your last example, with ?index=, then read the value of index in php from the global variable $_GET["index"].