CURL后的结果解析成数组问题

WBOY
Release: 2016-06-23 13:57:10
Original
1405 people have browsed it

index.php,echo json_encode后的结果为:
{"11":{"l_id":"11","l_title":"CITS-香港观光一天游【品质纯玩】"},"12":{"l_id":"12","l_title":"test"}}

然后客户端代码(client.php)如下:

$curlPost='key='.urlencode($key);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,'http://www.cits-sz.net/api/index.php');curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);$data=curl_exec($ch);curl_close($ch);echo json_decode($data,true);
Copy after login

得到的结果也是
{"11":{"l_id":"11","l_title":"CITS-香港观光一天游【品质纯玩】"},"12":{"l_id":"12","l_title":"test"}}

请问要如何才能将其变成如下的方式呢,谢谢了
Array
(
[11] => Array
(
[l_id] => 11
[l_title] => CITS-香港观光一天游【品质纯玩】
)

[12] => Array
(
[l_id] => 12
[l_title] => test
)

)



回复讨论(解决方案)

因为我想将得到的结果,再插入到数据库,就要用到
foreach($arr as $f)
但是,不是array形式的就没办法了

因为我想将得到的结果,再插入到数据库,就要用到
foreach($arr as $f)
但是,不是array形式的就没办法了


我的目的就是,有a,b两个站,a站上传了很多东西也有很多表,b站要用到a站的1,2两个表,我就想去获得a的1,2表,然后插入到b站的数据库里,所以在a站有http://www.cits-sz.net/api/index.php这个文件,然后在b站用client.php去获取进行相关的操作。

那你就再 json_decode 一次

$s = '{"11":{"l_id":"11","l_title":"CITS-香港观光一天游【品质纯玩】"},"12":{"l_id":"12","l_title":"test"}}';print_r(json_decode($s,1));
Copy after login
Array(    [11] => Array        (            [l_id] => 11            [l_title] => CITS-香港观光一天游【品质纯玩】        )    [12] => Array        (            [l_id] => 12            [l_title] => test        ))
Copy after login


很奇怪啊,我用这一句,curl_setopt($ch,CURLOPT_URL,'http://www.cits-sz.net/api/index.php');显示的就是序列化的
用这一句curl_setopt($ch,CURLOPT_URL,'localhost/szgl/api/index.php');这是本地就正常

为什么本地和上传到服务得到的结果不同呢??在本地就是array,在服务器就变成string了

访问 http://www.cits-sz.net/api/index.php
得:
无权限访问

我不好说什么了

访问 http://www.cits-sz.net/api/index.php
得:
无权限访问

我不好说什么了


直接访问这个不行的,后面有一个key的,
是这个
http://www.cits-sz.net/api/index.php?key=01f34958ba730edc2ca30d65245dd6a0

访问 http://www.cits-sz.net/api/index.php
得:
无权限访问

我不好说什么了


我把这个当成客户端的,暂时测试,地址为
http://www.cits-sz.net/api/client.php,里面的代码
<?phpinclude("../includes/application_top.php");$key='01f34958ba730edc2ca30d65245dd6a0';$curlPost='key='.urlencode($key);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,'http://www.cits-sz.net/api/index.php');//(1)//curl_setopt($ch,CURLOPT_URL,'http://localhost/szgl/api/index.php');//(2)curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);$data=curl_exec($ch);curl_close($ch);echo $a=json_decode($data,TRUE);//echo gettype($json);/*foreach($a as $f){	echo $f["l_title"];}*/?>
Copy after login

用(2)在本地就可以是array,用(1)就不行了,就是原始的信息。

访问 http://www.cits-sz.net/api/index.php
得:
无权限访问

我不好说什么了


api/index.php文件的代码如下:
<?phpinclude("../includes/application_top.php");function arrayRecursive(&$array, $function, $apply_to_keys_also = false){    static $recursive_counter = 0;    if (++$recursive_counter > 1000) {        die('possible deep recursion attack');    }    foreach ($array as $key => $value) {        if (is_array($value)) {            arrayRecursive($array[$key], $function, $apply_to_keys_also);        } else {            $array[$key] = $function($value);        }         if ($apply_to_keys_also && is_string($key)) {            $new_key = $function($key);            if ($new_key != $key) {                $array[$new_key] = $array[$key];                unset($array[$key]);            }        }    }    $recursive_counter--;}function JSON($array){	arrayRecursive($array, 'urlencode', true);	$json = json_encode($array);	return urldecode($json);}$key=Reqs("key");if ($key<>'01f34958ba730edc2ca30d65245dd6a0'){	echo '无权限访问';exit;}$sql='select l_id,l_title from '.TAB_TRAVEL_LINE.' order by l_id';$rsline=$mydb->query($sql,2);while ($fline=$rsline->movenext()){	$data_line[]=$fline;}//上面那几句是自己的读取数据的echo $a=JSON($data_line);?>
Copy after login

$key='01f34958ba730edc2ca30d65245dd6a0';$curlPost='key='.urlencode($key);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,'http://www.cits-sz.net/api/index.php');//(1)curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);$data=curl_exec($ch);curl_close($ch);print_r(json_decode(substr($data, 3), 1));//去掉 BOM 头后再解码
Copy after login
Copy after login
Array(    [0] => Array        (            [l_id] => 11            [l_title] => CITS-香港观光一天游【品质纯玩】        )    [1] => Array        (            [l_id] => 12            [l_title] => test        ))
Copy after login
Copy after login

$key='01f34958ba730edc2ca30d65245dd6a0';$curlPost='key='.urlencode($key);$ch=curl_init();curl_setopt($ch,CURLOPT_URL,'http://www.cits-sz.net/api/index.php');//(1)curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POST,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);$data=curl_exec($ch);curl_close($ch);print_r(json_decode(substr($data, 3), 1));//去掉 BOM 头后再解码
Copy after login
Copy after login
Array(    [0] => Array        (            [l_id] => 11            [l_title] => CITS-香港观光一天游【品质纯玩】        )    [1] => Array        (            [l_id] => 12            [l_title] => test        ))
Copy after login
Copy after login



非常感谢版主 xuzuning,问题完美解决,好人一生平安。

所以你一定不要有 BOM 头
这样事情就简单了

所以你一定不要有 BOM 头
这样事情就简单了



受教了,谢谢。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template