php - 淘宝手机号归属地api

查看:539
本文介绍了php - 淘宝手机号归属地api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

解决方案

-已解决

通过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);

这篇关于php - 淘宝手机号归属地api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆