欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

接收JSON问题

程序员文章站 2023-12-26 22:14:28
...
刚刚学会CURL提交JSON,
但是接收接口返回的数据不会了,会员提交登陆信息后,接口会返回数据,如果返回00就成功,返回11就是登陆失败。

成功记录登陆信息,然后跳转到其它网站。搞一下午都失败。请高手给我例子

万分感谢

回复讨论(解决方案)

提交的服务器端:
if(登录成功){
echo('00');
}else{//失败
echo('11');
}

你这边的程序:
$ch = curl_init();
$url="提交服务服务器地址和参数";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$a=curl_exec($ch);
curl_close($ch);
if($a=='00'){
成功了执行的代码;
}elseif($a=='11'){
登录失败
}

大体这样吧,不知道是不是你想的那样.

提交的服务器端:
if(登录成功){
echo('00');
}else{//失败
echo('11');
}

你这边的程序:
$ch = curl_init();
$url="提交服务服务器地址和参数";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$a=curl_exec($ch);
curl_close($ch);
if($a=='00'){
成功了执行的代码;
}elseif($a=='11'){
登录失败
}

大体这样吧,不知道是不是你想的那样.

接收两个参数A和B,当A=00代表登陆成功,取B里的URL跳转。是不是可以直接跳转到别的网址?

$arrayData = array("name" => "Hagrid", "age" => "36");$sendJsonData = json_encode($arrayData);$ch = curl_init('http://127.0.0.1/jietu/test.php');curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_POSTFIELDS, $sendJsonData);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array(		'Content-Type: application/json',		'Content-Length: ' . strlen($sendJsonData)));$ret = curl_exec($ch);if($ret == '00'){  成功了执行的代码;}elseif($ret == '11'){  登录失败}

$arrayData = array("name" => "Hagrid", "age" => "36");$sendJsonData = json_encode($arrayData);$ch = curl_init('http://127.0.0.1/jietu/test.php');curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_POSTFIELDS, $sendJsonData);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array(		'Content-Type: application/json',		'Content-Length: ' . strlen($sendJsonData)));$ret = curl_exec($ch);if($ret == '00'){  成功了执行的代码;}elseif($ret == '11'){  登录失败}


这本来是这样的写的,登录失败
{"aa":"00","bb":"http://www.xxx.com/index"}
写了半天要不取不到aa要么就取不到bb,只有当aa=00才是登录成功。刚摸索这块

var_dump($ret = curl_exec($ch));你输出你的值看看

var_dump($ret = curl_exec($ch));你输出你的值看看

boolean false

现在post数据成功,只是返回值{"aa":"00","bb":"http://www.xxx.com/index"}怎么取过来

json_decode

就像版主说的,返回值用json_decode转换下

json_decode

更迷茫。。。能来个例子吗?
现在我对接口提交数据成功了,怎么取回这个{"aa":"00","bb":"http://www.xxx.com/index"}

来点简单易懂了。。。刚摸索


var_dump($ret = curl_exec($ch));你输出你的值看看

boolean false

现在post数据成功,只是返回值{"aa":"00","bb":"http://www.xxx.com/index"}怎么取过来
$json_str = '{"aa":"00","bb":"http://www.xxx.com/index"}';
$arr = json_decode($json_str);
print_r($arr);

你 $ret = curl_exec($ch) 后
echo $ret; 得到 {"aa":"00","bb":"http://www.xxx.com/index"}
表示你确实接收到了一个 json 串
那么

$t = json_decode($ret);print_r($t);/*stdClass Object(    [aa] => 00    [bb] => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00    [bb] => http://www.xxx.com/index)*/

你都在用 json 了,怎么连 json 相关的函数都不知道?

你 $ret = curl_exec($ch) 后
echo $ret; 得到 {"aa":"00","bb":"http://www.xxx.com/index"}
表示你确实接收到了一个 json 串
那么

$t = json_decode($ret);print_r($t);/*stdClass Object(    [aa] => 00    [bb] => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00    [bb] => http://www.xxx.com/index)*/

你都在用 json 了,怎么连 json 相关的函数都不知道?

放然大悟。。。就这么简单.万分感谢,准备结贴,感谢版主和码无边
相关标签: 接收JSON问题

上一篇:

下一篇: