简体   繁体   English

PHP无需查询即可获取URL

[英]PHP fetches URL without query

I am trying to fetch from a service using jQuery and PHP (as a Proxy). 我试图从使用jQuery和PHP(作为代理)的服务中获取。

Here is my PHP that fetches the JSON. 这是我的PHP,用于获取JSON。

<?php 

    if (!isset($_GET['url'])) die();
    $url =  urldecode($_GET['url']);
    $url = 'http://' . str_replace('http://', '', $url);
    echo file_get_contents($url);

 ?>

With the JS to manipulate the data (Sorry had to remove the key): 使用JS来处理数据(抱歉,必须删除密钥):

var api ='proxy.php?url=http://api.buycraft.net/v3?secret=MY-SECRET-KEY&action=payments';

$.getJSON(api, function(data){
     $.each(data, function(i, donor){
       console.log(donor);
     });
}); 

So going to proxy.php?url=http://api.buycraft.net/v3?secret=MY-SECRET-KEY&action=payments only returns the following: 因此,转到proxy.php?url=http://api.buycraft.net/v3?secret=MY-SECRET-KEY&action=payments仅返回以下内容:

{"code":100,"payload":[]}

But if I visit the JSON directly, I can see the data that I want 但是,如果我直接访问JSON,则可以看到所需的数据

{
"code": 0,
"payload": [
{
"time": 1349661897,
"packages": [
"49381"
],
"ign": "notch",
"price": "15.99",
"currency": "USD"
}

For example. 例如。

And I know it's because php removes the ?action=payments query. 而且我知道这是因为php删除了?action=payments查询。 Even if I use &amp; 即使我使用&amp; instead of & . 代替& So is there a way to keep PHP from removing the query from the URL? 那么有没有一种方法可以防止PHP从URL中删除查询?

Keep the API key in your php and don't expose it to client side in your javascript for obvious security reasons. 将API密钥保留在您的php中,出于明显的安全原因,请勿将其公开给javascript的客户端。 You might just as well store the whole URL (other than proxy.php ) in your php config. 您也可以将整个URL( proxy.php )存储在php配置中。

Also should implement use of CURL to retireve data. 还应实现使用CURL检索数据。 Allows for some error handling so you can send info back to client should API not be available 允许进行一些错误处理,以便在API不可用时可以将信息发送回客户端

Try removing this from the backend PHP script: 尝试从后端PHP脚本中删除它:

$url =  urldecode($_GET['url']);

PHP performs that automatically for you. PHP将自动为您执行该操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM