douzhu6149 2015-01-01 18:49
浏览 84
已采纳

使用列表函数(?)无法正确传递变量

Here's a snippet I use the gather some data in JSON from a URL specifying 2 variables:

<?php

function soldipubblici() {

    $curl_parameters = array(
        'codicecomparto'    =>  "PRO",
        'codiceente'        =>  "011120674",
    );

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,"http://soldipubblici.gov.it/it/ricerca");
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query( $curl_parameters ));
    curl_setopt($ch,CURLOPT_HTTPHEADER,array (
        "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
        "Accept: application/json",
        "X-Requested-With: XMLHttpRequest",
    ));

    $output=curl_exec($ch);

    curl_close($ch);
}

echo soldipubblici();

?>

As you can notice the variables are manually set. So I put up a simple web page with 2 options. The input tag is just for testing the POST data.

<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form>
<label>Seleziona l'ente</label>
<select name="data" id="data">
    <option value="PRO - 011142764">COMUNE DI AGLIE'</option>
    <option value="PRO - 011120674">COMUNE DI AGRATE CONTURBIA</option>
</select>
<input type="text" name="textfield" id="textfield" />
</form>

<script>
$('#data').change(function() {
    $.post("richiesta.php", { value: this.value });
    $('#textfield').val(this.value);
});
</script>
</body>
</html>

And this is richiesta.php, that receives the POST data on change event (I followed this thread):

<?php
list($comparto, $ente) = explode("-", $_POST['value'], 2);
echo "comparto: $comparto, ente: $ente";
?>

The request works fine, on change event I successfully get the POST value:

enter image description here

Since it worked I can now place in richiesta.php my "real" function with the two variables, this time not manually but retrieving the values from the POST request:

<?php

function soldipubblici() {
    list($comparto, $ente) = explode("-", $_POST['value'], 2);

    $curl_parameters = array(
        'codicecomparto'    =>  $comparto,
        'codiceente'        =>  $ente,
    );

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,"http://soldipubblici.gov.it/it/ricerca");
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query( $curl_parameters ));
    curl_setopt($ch,CURLOPT_HTTPHEADER,array (
        "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
        "Accept: application/json",
        "X-Requested-With: XMLHttpRequest",
    ));

    $output=curl_exec($ch);

    curl_close($ch);
}

echo soldipubblici();

?>

Unfortunately it doesn't work :( I get an empty response in Firebug... If in the same snippet I set manually the 2 variables instead if works... seems like they're not passed correctly from the list function.

  • 写回答

1条回答 默认 最新

  • dongsong4418 2015-01-01 18:54
    关注

    There are spaces around the - in the options, you probably need to remove them.

    $curl_parameters = array(
        'codicecomparto' => trim($comparto),
        'codiceente' => trim($ente),
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog