user5281122
user5281122

Reputation:

AJAX Call to PHP not returning any value

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.

Upvotes: 1

Views: 512

Answers (2)

Wesley Smith
Wesley Smith

Reputation: 19561

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.

Upvotes: 1

Eddsters
Eddsters

Reputation: 497

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);

?>

Upvotes: 0

Related Questions