• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

接收JSON问题

php 搞代码 3年前 (2022-01-23) 17次浏览 已收录 0个评论
文章目录[隐藏]

刚刚学会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才是登录本文来源gao.dai.ma.com搞@代*码#网搞代gaodaima码成功。刚摸索这块

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     => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00     => 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     => http://www.xxx.com/index)*/$t = json_decode($ret, 1);print_r($t);/*Array(    [aa] => 00     => http://www.xxx.com/index)*/

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

放然大悟。。。就这么简单.万分感谢,准备结贴,感谢版主和码无边


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:接收JSON问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址