1

I'm trying to use AJAX to call a function in a PHP file.

The function basically takes data that is submitted in the AJAX call, makes a couple other requests to API's, and gets a JSON object from an external API.

I want to send that JSON object back to my page, to be acted on by javascript.

Here is my code:

function secureGet(sumNam){

    $.ajax({
        dataType: "json",
        type: 'POST',
        data: {sumNam: sumNam},
        url: 'get_score.php',
        success: function (json, state) {
            console.log(state);
            statsObject = json;
            console.log(statsObject);
        }
    })
}

PHP:

    <?php
require_once 'apikey.php';

if(isset($_POST['sumNam'])){
$name = $_POST['sumNam'];
secureProxy($name);
}

function secureProxy($summoner_name){

$url_one = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" . $summoner_name . "?api_key=" . $api_key;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url_one);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result_one = curl_exec($ch);
    curl_close($ch);

$json_array = json_decode($result_one, true);

$summoner_id = $json_array[$summoner_name]['id'];

$url_two = "https://na.api.pvp.net/api/lol/na/v2.2/matchhistory/" . $summoner_id .  "?rankedQueues=RANKED_SOLO_5x5,RANKED_TEAM_5x5&beginIndex=0&endIndex=10&api_key=" . $api_key;

    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch2, CURLOPT_URL, $url_two);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    $result_two = curl_exec($ch2);
    curl_close($ch2);

print_r($result_two);

}

?>

I think the call is successfully being made because in my console it says:

iGET XHR  http://127.0.0.1/b2p/get_score.php [HTTP/1.1 200 OK 1551ms]

But the console isn't logging any information on the returned object.

Any reason as to why this might be happening?

Thanks!

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: api_key in C:\wamp\www\b2p\get_score.php on line <i>12</i>

...this goes on for a while

{"status": {"message": "Missing api key", "status_code": 401}}

But the API call works when I do it on its own.

24
  • 1
    you specified dataType: "json", in your ajax call, but in php you are doing print_r($result_two); which does not return json. Per the docs - api.jquery.com/jquery.ajax - ... The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. .... You could try echo json_encode($result_two);, depending on the value of $result_two. Commented Aug 30, 2015 at 4:51
  • 1
    So the code:200 tells you that you that ajax successfully found the page, but the SyntaxError: JSON.parse: tells you that your returned data was invalid json. Since the error message is unexpected character at line 1 column 1 of the JSON data it means that the returned data does not start with [ or {. If $result_two is valid json already, you can just echo it directly. Commented Aug 30, 2015 at 5:09
  • 1
    @CookieCoder what your are seeing are php notices, or warnings. If your have error_reporting enabled, they will echo the warnings out in any response that is sent right at the top. That will effectively invalidate any json you are sending back causing the 200 error, the error says your missing an API key somewhere, id start there, it will also give you the file name and line number Commented Aug 30, 2015 at 5:13
  • 1
    Where is that defined? Keep in mind that PHP functions (unlike javascript) create their own (variable scope)[php.net/manual/en/language.variables.scope.php] and can only see variables that are either passed into the function or are defined within the function itself Commented Aug 30, 2015 at 5:21
  • 1
    Got it! I wasnt passing the api key into the function. It was basically not in the scope of the function:) Commented Aug 30, 2015 at 5:22

2 Answers 2

1

What your are seeing are php notices, or warnings. If you have error_reporting enabled, they will echo the warnings out in any response that is sent right at the top. That will effectively invalidate any json you are sending back causing the 200 error, the error says your missing an API key somewhere, id start there, it will also give you the file name and line number.

Go to C:\wamp\www\b2p\get_score.php and check line 12, whatever you are doing there requires an API key and you have not supplied one.

It's very likely this line $url_one = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" . $summoner_name . "?api_key=" . $api_key;.

Do you define $api_key before this line executes?

Keep in mind that PHP functions (unlike javascript) create their own variable scope and can only see variables that are either passed into the function or are defined within the function itself.

You need to pass your API key variable into the function when you call it.

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

Comments

0

I think it is not returning anything because in your PHP you haven't set the type you are returning. Since you will be returning in Json format, you have to set your PHP header as below:

    header('Content-type: application/json');

    echo json_encode($result_two);

?>

Comments

Your Answer

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