Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javaScript向PHP脚本传递参数_Javascript_Php - Fatal编程技术网

从javaScript向PHP脚本传递参数

从javaScript向PHP脚本传递参数,javascript,php,Javascript,Php,我使用PHP脚本从API获取数据,并从javaScript文件调用该脚本,该文件在加载DOM时即在浏览器加载DOM后加载。我正在开发的网站有多个页面,每个页面都有一些从API获取的数据。例如,一个页面具有从中提取的数据https://api.twitter.com/1.1/trends/place.json?id=1 而另一个页面B具有从id为2的API获取的数据 我的PHP脚本是:- <?php require_once('TwitterAPIExchange.php'); $set

我使用PHP脚本从API获取数据,并从javaScript文件调用该脚本,该文件在加载DOM时即在浏览器加载DOM后加载。我正在开发的网站有多个页面,每个页面都有一些从API获取的数据。例如,一个页面具有从中提取的数据https://api.twitter.com/1.1/trends/place.json?id=1 而另一个页面B具有从id为2的API获取的数据

我的PHP脚本是:-

<?php
require_once('TwitterAPIExchange.php');


$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);


$url = 'https://api.twitter.com/1.1/trends/place.json';
$getfield = '?id=1';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();
?>
在获取数据之后,调用appendData,这是我已经编写的


在我的网站上,我有大约30个页面用不同的id调用这个API。我不想在我的web服务器上有30个PHP脚本。使用javascript获取调用PHP脚本时,有没有一种方法可以将id传递给PHP脚本。

最简单的方法是向从中获取的URL添加一个参数:

fetch('getData.php?id=1').then(...)
在getData.php脚本中,检查$\u GET[]数组中的参数:

fetch('getData.php?id=1').then(...)
if (!isset($_GET['id']) {
    // Do something by default
} else {
    switch($_GET['id']) {
       case '1':
            // do stuff
            break;
       case '2':
            // do different stuff
            break;
    }
}