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

我使用php curl使用Ajax从我的javascript中生成一个http get long polling请求。 这是来自javascript的调用</ p>

  var i; 
i ++;
$ .ajax({
url:“http://localhost/myport.php”,\ n类型:GET,
成功:函数(响应){...},
...
...
</ code> </ pre>

这是我如何 在myport.php文件中进行php调用</ p>

 &lt;?php 
$ ch = curl_init();
$ curl_setopt($ ch,CURLOPT_URL,“http: // localhost:7555 / test?index =“//这里我需要在上面的JS中设置一个值(变量i)
</ code> </ pre>

如果我做了 直接从js调用,我会做</ p>

  $ .ajax({url:“http:// localhost:7555 / test?index =”+ i 
</ code > </ pre>

我是php和curl的新手,我想知道如何传递该变量的值,以便我可以获得该调用的参数。</ p>
</ DIV>

展开原文

原文

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.

2个回答



如果我理解正确,你只想将变量 $ i </ code>的值附加到cURL 打电话,你要这样做:</ p>

 &lt;?php 
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,“http:// localhost: 7555 / test?index =“。$ i);
</ code> </ pre>

甚至,</ p>

  curl_setopt($ ch  ,CURLOPT_URL,sprintf(“http:// localhost:7555 / test?index =%d”,$ i)); 
</ code> </ pre>

另请注意帽子没有 函数调用前的 $ </ code>:它是 curl_setopt()</ code>而不是 $ curl_setopt()</ code>( $ </ code>用于变量,如 $ ch </ code>)。</ p>

编辑</ strong> </ p>

在澄清问题时,似乎你 需要从JavaScript到PHP获取此 i </ code>变量。 您可以在AJAX调用中将其作为 GET </ code>参数传递:</ p>

  var i; 
i ++;
$ .ajax({
url :“http://localhost/myport.php?index =”+ i,
type:GET,
success:function(response){...},
...
...
</ code> </ pre>

然后,从PHP中,您可以像这样使用它:</ p>

  curl_setopt($ ch,CURLOPT_URL,sprintf(  “http:// localhost:7555 / test?index =%d”,$ _GET ['index'])); 
</ code> </ pre>

您还应验证< 代码> $ _ GET ['index'] </ code>实际传入:</ p>

  if(!isset($ _ GET ['index']))
{\ n die(“索引未指定!”);
}
</ code> </ pre>
</ div>

展开原文

原文

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!");
}

douweicheng5532
douweicheng5532 如果我不清楚,我很抱歉。 我想从Javascript获取curl调用的参数。 我怎样才能做到这一点? 正如我所示,ajax正在调用php,然后调用http get请求。
5 年多之前 回复



你可以像上一个例子一样从JavaScript传递 i </ code>,?index = </ code>,然后从全局变量 $ _ GET [“index”] </ code>中读取php中 index </ code>的值。</ p>
</ div>

展开原文

原文

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"].

立即提问
相关内容推荐