2015-07-10 64 views
1

我正在使用PHP卷曲使HTTP使用Ajax从我的JavaScript获取长轮询请求。这是从JavaScript从JavaScript传递动态参数通过AJAX在cURL调用

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

这里呼叫我如何让PHP调用在myport.php文件

<?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 

如果我直接从爵士打出电话,我会做

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

我是新来的PHP和卷曲,我想知道如何传递该变量的值,所以我可以得到一个参数的调用。

回答

0

如果我理解正确的话,而你只是想将变量$i的价值附加到卷曲电话,你可以这样做:

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

甚至,

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

而且在函数调用之前没有$:它是curl_setopt()而不是$curl_setopt()$用于变量,如$ch)。

编辑

在澄清了问题,看来你需要从JavaScript得到这个i变量PHP。你可以把它作为一个GET参数在你的AJAX调用:

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

然后,PHP,你可以使用它像这样:

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

你也应该确认$_GET['index']在实际传递:

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

如果我不清楚,我很抱歉。我想从Javascript获取curl调用的参数。我怎样才能做到这一点?正如我所展示的,ajax调用了php,然后调用http get请求。 –

0

您可以从JavaScript传递i就像在你的最后一个例子,与?index=,然后读取的值在全局变量$_GET["index"]的php中使用。