欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

淘宝手机号归属地api

http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443

直接用file_get_contents 返回结果是这样的

Plain Text code
 
?
1
2
3
4
5
6
7
8
9
__GetZoneResult_ = {
    mts:'1585078',
    province:'江苏',
    catName:'中国移动',
    telString:'15850781443',
    areaVid:'30511',
    ispVid:'3236139',
    carrier:'江苏移动'
}


应该采用什么方式使用这个接口?

如果直接使用这个file_get_contents  得到的结果应该如何处理 才能得到 归属地?








https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13001631234

使用这个接口查询归属地信息后php不能够解析json字符串
php

    $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);

3个回答

0
采纳

-已解决

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

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

    $res = trim(explode('=',$res)[1]);
    $res = iconv('gbk','utf-8', $res);
    $res = str_replace("'",'"', $res);
    $res = preg_replace('/(\w+):/is', '"$1":', $res);
0

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

 
下面是官方文档的说明:
<?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

?>
来自  https://segmentfault.com/q/1010000005672227
普通分类: