Heim >Backend-Entwicklung >PHP-Tutorial >CURL后的结果解析成数组问题
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);
因为我想将得到的结果,再插入到数据库,就要用到
foreach($arr as $f)
但是,不是array形式的就没办法了
因为我想将得到的结果,再插入到数据库,就要用到
foreach($arr as $f)
但是,不是array形式的就没办法了
那你就再 json_decode 一次
$s = '{"11":{"l_id":"11","l_title":"CITS-香港观光一天游【品质纯玩】"},"12":{"l_id":"12","l_title":"test"}}';print_r(json_decode($s,1));
Array( [11] => Array ( [l_id] => 11 [l_title] => CITS-香港观光一天游【品质纯玩】 ) [12] => Array ( [l_id] => 12 [l_title] => test ))
为什么本地和上传到服务得到的结果不同呢??在本地就是array,在服务器就变成string了
访问 http://www.cits-sz.net/api/index.php
得:
无权限访问
我不好说什么了
访问 http://www.cits-sz.net/api/index.php
得:
无权限访问
我不好说什么了
访问 http://www.cits-sz.net/api/index.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"];}*/?>
访问 http://www.cits-sz.net/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);?>
$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 头后再解码
Array( [0] => Array ( [l_id] => 11 [l_title] => CITS-香港观光一天游【品质纯玩】 ) [1] => Array ( [l_id] => 12 [l_title] => test ))
$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 头后再解码
Array( [0] => Array ( [l_id] => 11 [l_title] => CITS-香港观光一天游【品质纯玩】 ) [1] => Array ( [l_id] => 12 [l_title] => test ))
所以你一定不要有 BOM 头
这样事情就简单了
所以你一定不要有 BOM 头
这样事情就简单了