获取url以获取带有用户名和密码的json数据,如何在javascript源代码显示用户名和密码时对其进行安全保护


Got url to fetch json data with username and password, how to secure it as javascript source shows username and password

(我在PHP中使用json)

嗨,我有一个网址,来自某个网络服务,上面写着:

http://www.example.com?username=$username&password=$password&fname=名字

现在他们说我可以用xml或json获取数据来检索数据,同时传递我的用户名和密码。当我在谷歌上搜索时发现json是更好的选择。现在我已经完成了json,并成功地接收到了所需的数据。

但我可以在页面的视图源代码中看到我用来获取的所有javascript。用户名和密码也是如此。

当涉及到使用json或者说使用REST.获取数据时,我是一个新的程序员

有人能告诉我基本想法吗?这对我来说就足够了。

然后您需要做的就是用PHP获取该URL。为此,cURL是最常用的:

$parameters = array(
    'username'=>$username,
    'password'=>$password,
    'fname'=>$fname
);
$ch = curl_init('http://www.example.com?' . http_build_query($parameters);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

您应该在代码中看到该URL中的数据。现在,由于它是JSON,我们需要解码:

print_r(json_decode($output));

您应该看到一个PHP对象,在那里您可以根据需要访问它的属性。