Robert
Robert

Reputation: 925

How to send a data through json

Hi guys i have been trying to use cURL to send and retrieve data.

I am new to php as only been doing this for about 3 weeks.

I have setup 2 variables from the database and when called from another site through cURL it should send them using json but for some reason it is not sending the data,

Could someone please tell me what i am doing wrong, everything works fine untill it gets to the json section and then it just does nothing.

code:

if(isset($_REQUEST['token'])){

$token = $_REQUEST['token'];
$url = $_REQUEST['www'];

$token = trim(htmlentities($token));
$safetoken = mysql_real_escape_string($token);
$url = trim(htmlentities($url));
$safeurl = mysql_real_escape_string($url);

$checkwebsite = "SELECT message,islive FROM websitetokens WHERE url='".$safeurl."' AND token='".$safetoken."'";
$checkwebsite_result = mysql_query($checkwebsite) OR die();
$numberofrows = mysql_num_rows($checkwebsite_result);

if($numberofrows > 0){

    $website = mysql_fetch_array($checkwebsite_result);
    $message = stripslashes($website["message"]);
    $islive = stripslashes($website["islive"]);

    json_encode(array('message' => $message,'islive' => $islive,));

    $date = date('Y-m-d');
    $time = gmdate('H:i');

    $loginwebsite = "UPDATE websitetokens SET loggedin='".$date."',time='".$time."' WHERE url='".$safeurl."' AND token='".$safetoken."'";
    $loginwebsite_result = mysql_query($loginwebsite) OR die(mysql_error());

} else {

    json_encode(array('message' => '','islive' => '1',));

}

}

Thanks

Upvotes: 0

Views: 243

Answers (3)

mdprotacio
mdprotacio

Reputation: 842

there will be an output if you echo the result of json_encode. Also, it may also be wise if you set the content type of the output like so:

header("Content-Type:application/json");
echo json_encode(array('message' => '','islive' => '1',));

Upvotes: 0

kitti
kitti

Reputation: 14794

json_encode returns a string with the data in JSON format. Add echo to those lines (i.e. echo json_encode(array( ...)

Upvotes: 0

Marc B
Marc B

Reputation: 360602

json_encode() returns the data, it doesn't do output, try using

echo json_encode(...);

instead.

Upvotes: 1

Related Questions