anbu anbu - 27 days ago 5
PHP Question

REST Ful services error "Call to undefined function deliver_response() "

I just started out with restful web services.

I am getting an error of "Call to undefined function deliver_response() " please help me solve this problem or give me advise.
The function call is not working, I am new to restful web services and before I go into restful web services, I want to know why this function call is not working.

I am having values of score=400 level=4 gammer_id=test1@gmail.com

//rest.php

<?php
include "..\dbconn.php";
include "rest_function.php";
$gamer_details = array();

if(!empty($_GET['gamer_id'])){
echo $id = $_GET['gamer_id'];

$gamer_details = get_gamer_details($id);
//print_r($gamer_details);


if(!empty($gamer_details)){

$score = $gamer_details['score'];
$level = $gamer_details['level'];
$gamer_id = $gamer_details['gamer_id'];

deliver_response($score,$level,$gamer_id);

}else{
echo "failed";
}

function deliver_response($score,$level,$gamer_id){

header('HTTP/1.1 $score $level $gamer_id');
$response_value['score']=$score;
$response_value['level']=$level;
$response_value['gamer_id']=$gamer_id;
$json_response = json_encode($response_value);
echo $json_response;
}

}


?>

Answer Source

You use "deliver_response" before you implemented it. Implement the function before you use it:

<?php 
include "..\dbconn.php";
include "rest_function.php";

$gamer_details = array();

function deliver_response($score,$level,$gamer_id){
    header('HTTP/1.1 $score $level $gamer_id');
    $response_value['score']=$score;
    $response_value['level']=$level;
    $response_value['gamer_id']=$gamer_id;
    $json_response = json_encode($response_value);
    echo $json_response;
}

if(!empty($_GET['gamer_id'])){
    echo $id = $_GET['gamer_id'];

    $gamer_details  = get_gamer_details($id);
    //print_r($gamer_details);
    if(!empty($gamer_details)){
        $score = $gamer_details['score'];
        $level = $gamer_details['level'];
        $gamer_id = $gamer_details['gamer_id'];
        deliver_response($score,$level,$gamer_id);
    } else {
        echo "failed";
    }
}
?>