为什么我的卷曲请求不起作用我在做什么错

时间:2019-03-09 18:28:24

标签: php jquery wordpress curl php-curl

我在做什么错了?

我正在尝试从下拉列表中进行新的卷曲请求,我做错了什么?

<form action="" method="POST">
     <div class="form-group">
        <label for="comment">Select Page:</label>
        <select class="form-control" name="pagecall" id="pagecall" style="width:75%;float:right;" onchange="davsele(this.value);">
            <?php
                $totalPage = $dataget['total'] / 20;
                for($i=1;$i<=$totalPage;$i++)
                {
                    $selc = ($getPageCount==$i)?'selected':'';
                    echo "<option value=".$i." $selc>Page No. ".$i."</option>";
                }
            ?>
        </select>
    </div>
</form>

我尝试调用的功能

<?php

    function proCall() {

    $ch = curl_init();

    $getPageCount == sval;

    curl_setopt($ch, CURLOPT_URL,"https://products-export-api.g2a.com/v1/products?page=$getPageCount");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);

        //Gets the API data
    $response = curl_exec($curl);
    $err = curl_error($curl);
    //curl_close($curl);
    $dataget = json_decode($response,true);
    print_r($dataget);
    echo "test";
    curl_close ($ch);
    }
?>

从下拉列表发送到js函数的内容

function davsele(sval)
{

var result = "<?php proCall();?>";
alert(result);
return false;
}

1 个答案:

答案 0 :(得分:0)

您正在以某种方式混淆Javascript和PHP,这是行不通的。您尝试在PHP函数中访问JS-Variable(sval),但该方法无效。

您需要使用AJAX请求在服务器上调用PHP函数,并将JS变量作为GET参数传递。

因此,您应该将PHP代码放在文件中(例如api.php),然后在JS函数中调用它:

function davsele(sval) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      alert(this.responseText);
    }
  };
  xhttp.open("GET", "api.php?value=" + sval, true);
  xhttp.send();
}

api.php的内容可能如下所示:

<?php
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, 'https://products-export-api.g2a.com/v1/products?page=' . $_GET['value']);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($curl);

    //Gets the API data
    $response = curl_exec($curl);
    $err = curl_error($curl);
    //curl_close($curl);
    $dataget = json_decode($response,true);
    print_r($dataget);
    echo "test";
    curl_close ($curl);
?>