淘宝手机号归属地api

WBOY
发布: 2016-07-06 13:54:11
原创
2000 人浏览过

https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13001631234
使用这个接口查询归属地信息后php不能够解析json字符串
php

<code>    $ch = curl_init();
    $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=$mobile"."&t=".time();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch, CURLOPT_URL, $url);
    $res = curl_exec($ch);
    $res = trim(explode('=',$res)[1]);
    $res = iconv('gbk','utf-8', $res);
    var_dump($res);
    $res = json_decode($res, true);
    var_dump($res);</code>
登录后复制
登录后复制

淘宝手机号归属地api

回复内容:

https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13001631234
使用这个接口查询归属地信息后php不能够解析json字符串
php

<code>    $ch = curl_init();
    $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=$mobile"."&t=".time();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch, CURLOPT_URL, $url);
    $res = curl_exec($ch);
    $res = trim(explode('=',$res)[1]);
    $res = iconv('gbk','utf-8', $res);
    var_dump($res);
    $res = json_decode($res, true);
    var_dump($res);</code>
登录后复制
登录后复制

淘宝手机号归属地api

-已解决

通过json_last_error()发现是JSON_ERROR_SYNTAX: ' - Syntax error, malformed JSON'

后来将key接用""扩了起来就可以了

<code>    $res = trim(explode('=',$res)[1]);
    $res = iconv('gbk','utf-8', $res);
    $res = str_replace("'",'"', $res);
    $res = preg_replace('/(\w+):/is', '"$1":', $res);</code>
登录后复制

淘宝手机号归属地api
建议换个接口,或者是请求缺少某参数,返回的不是json数据

对于php来说jsonpjson,并非直接取后就完事了。

<code>下面是官方文档的说明:
<?php // the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null

?></code>
登录后复制

官方文档入口json-decode

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!