0

I am trying to fetch from a service using jQuery and PHP (as a Proxy).

Here is my PHP that fetches the 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):

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:

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

But if I visit the JSON directly, I can see the data that I want

{
"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. Even if I use &amp; instead of &. So is there a way to keep PHP from removing the query from the URL?

2 Answers 2

1

Keep the API key in your php and don't expose it to client side in your javascript for obvious security reasons. You might just as well store the whole URL (other than proxy.php) in your php config.

Also should implement use of CURL to retireve data. Allows for some error handling so you can send info back to client should API not be available

Sign up to request clarification or add additional context in comments.

2 Comments

should be able to simply use it as is from there, no url_encoding needed
Thanks for reminding me. Just wrapped it in a php function and called it a day.
0

Try removing this from the backend PHP script:

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

PHP performs that automatically for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.