简体   繁体   English

如何通过JSON发送数据

[英]How to send a data through json

Hi guys i have been trying to use cURL to send and retrieve data. 嗨,大家好,我一直在尝试使用cURL发送和检索数据。

I am new to php as only been doing this for about 3 weeks. 我是php的新手,因为它只进行了大约3周。

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, 我从数据库中设置了2个变量,当通过cURL从另一个站点调用它时,它应该使用json发送它们,但由于某种原因,它没有发送数据,

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. 有人可以告诉我我在做什么错,一切正常,直到到达json部分,然后它什么也不做。

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 谢谢

json_encode() returns the data, it doesn't do output, try using json_encode()返回数据,但不输出,请尝试使用

echo json_encode(...);

instead. 代替。

json_encode returns a string with the data in JSON format. json_encode返回带有JSON格式数据的字符串。 Add echo to those lines (ie echo json_encode(array( ...) 将echo添加到这些行(即echo json_encode(array(...)

there will be an output if you echo the result of json_encode . 如果您echojson_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',));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM