如何用php调用外部接口json数据?
ringa_lee
ringa_lee 2017-04-11 09:00:16
0
4
6626

json数据文件格式如下,从链接中可以获取:http://api.dataoke.com/index....
这些数据都是从网站接口过来的,我的网站使用Php原生态写的,怎么把这些数据输出到前台html里面去呢?
{

"data": {
    "api_type": "\u9886\u5238\u4f18\u60e0v1.4",
    "update_time": "2016\/12\/25 16:45:11",
    "total_num": "17398",
    "update_content": "\u5168\u7ad9\u63a5\u53e3\u5df2\u5347\u7ea7\u4e3a\u5206\u9875\u6a21\u5f0f\uff0c\u6bcf\u9875200\u6761\u6570\u636e\uff0c\u5206\u9875\u53c2\u6570\uff1a&page"
},
"result": [{
    "ID": "1766196",
    "GoodsID": "43642544442",
    "Title": "\u5899\u8d34\u5367\u5ba4\u5ba2\u5385\u7384\u5173\u8d70\u5eca\u73bb\u7483\u79fb\u95e8\u80cc\u666f\u88c5\u9970\u9632\u6c34\u81ea\u7c98\u521b\u610f\u53ef\u79fb\u9664\u8d34\u753b\u7eb8",
    "D_title": "\u5899\u8d34\u5367\u5ba4\u5ba2\u5385\u7535\u89c6\u5899 \u88c5\u9970\u9632\u6c34\u521b\u610f\u8d34\u753b\u7eb8",
    "Pic": "http:\/\/img.alicdn.com\/imgextra\/i4\/TB1xYqYHXXXXXXQXFXXXXXXXXXX_!!0-item_pic.jpg",
    "Cid": "4",
    "Org_Price": "16.80",
    "Price": 11.8,
    "IsTmall": "1",
    "Sales_num": "236",
    "Dsr": "4.8",
    "SellerID": "2032858462",
    "Commission": "20.50",
    "Commission_jihua": "20.50",
    "Commission_queqiao": "0.00",
    "Jihua_link": "http:\/\/pub.alimama.com\/myunion.htm?spm=a220o.1000855.0.0.vxengJ#!\/promo\/self\/campaign?campaignId=38974934&shopkeeperId=54345479&userNumberId=2032858462&tab=2",
    "Que_siteid": "0",
    "Jihua_shenhe": "0",
    "Introduce": "\u7b80\u7ea6\u73b0\u4ee3\uff0c\u5c45\u5bb6\u5fc5\u5907\u5899\u8d34\uff0c\u8272\u6cfd\u6e29\u548c\u800c\u9971\u6ee1\uff0c\u753b\u8d28\u7ec6\u817b\u7cbe\u7f8e\uff0c\u4f18\u60e0\u6765\u88ad\uff0c\u901f\u62a2\u3002",
    "Quan_id": "7f26195053154e7a866691bafeb698b5",
    "Quan_price": "5.00",
    "Quan_time": "2016-12-28 00:00:00",
    "Quan_surplus": "9540",
    "Quan_receive": "460",
    "Quan_condition": "\u5355\u7b14\u6ee111\u5143\u53ef\u7528\uff0c\u6bcf\u4eba\u9650\u98863 \u5f20",
    "Quan_m_link": "http:\/\/dwz.cn\/4UikTF",
    "Quan_link": "http:\/\/shop.m.taobao.com\/shop\/coupon.htm?seller_id=2032858462&activity_id=7f26195053154e7a866691bafeb698b5"
}, ......
ringa_lee
ringa_lee

ringa_lee

全部回复(4)
黄舟
function get($url){
    $ch = curl_init();

  //设置选项,包括URL
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);

  //执行并获取HTML文档内容
  $output = curl_exec($ch);

  //释放curl句柄
  curl_close($ch);
    return $output;

}

$content=get('http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=0vutvy4tsg&v=2&page=1');
$data=json_decode($content,true);//$data就是最终的数据 而且已经转换成了数组
刘奇

curl去请求这个地址过去啊

PHPzhong
$url="http://api.dataoke.com/index.phpr=Port/index&type=total&appkey=0vutvy4tsg&v=2&page=1";
$content = file_get_contents($url);
$data = json_decode($content,true);
巴扎黑

可以自己封装个方法 主要是curl方法

/**
 * 发送HTTP请求方法
 * @param  string $url    请求URL
 * @param  array  $params 请求参数
 * @param  string $method 请求方法GET/POST
 * @return array  $data   响应数据
 */
function httpCurl($url, $params, $method = 'POST', $header = array(), $multi = false){
    date_default_timezone_set('PRC');
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header,
            CURLOPT_COOKIESESSION  => true,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_COOKIE         =>session_name().'='.session_id(),
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
           // $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
           // 链接后拼接参数  &  非?
            $opts[CURLOPT_URL] = $url . '&' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式!');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误:' . $error);
    return  $data;
 }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板