商品比价API及PHP示例

浏览:2477 发布日期:2016/12/15 分类:技术分享
商品比价API汇集了所有主流网上商城的报价、促销、抢购、团购信息
商品比价API地址:https://www.juhe.cn/docs/api/id/137/
1.支持商城列表查询
接口地址:http://japi.juhe.cn/manmanmai/allsites.from
支持格式:json
请求方式:get
请求示例:http://japi.juhe.cn/manmanmai/allsites.from?key=YOURKEY
JSON返回示例:{
    "error_code": 0,
    "reason": "success",
    "result": [
        {
            "Id": "1",
            "Site": "京东商城"
        }
    ]
}
2.简单比价模式查询
接口地址:http://japi.juhe.cn/manmanmai/simple.from
请求示例:http://japi.juhe.cn/manmanmai/simple.from?key=YOURKEY&keyword=%E5%B0%8F%E7%B1%B3
JSON返回示例:{
    "error_code": 0,
    "reason": "success",
    "result": [
        {
            "sppic": "http://img12.360buyimg.com/n1/jfs/t1828/117/90430867/76399/1a519b48/55cc56e8N05d7bd8a.jpg",
            "siteName": "京东商城",
            "spurl": "http://item.jd.com/1743177.html",
            "spname": "【套装版】小米 红米Note 2 白色 移动4G手机 双卡双待",
            "spprice": "849.00"
        }
    ]
}
3.比价复杂模式查询
接口地址:http://japi.juhe.cn/manmanmai/complex.from
请求示例:http://japi.juhe.cn/manmanmai/complex.from?key=YOURKEY&keyword=%E5%B0%8F%E7%B1%B3&PageSize=6&PageNum=10
JSON返回示例:{
    "error_code": 0,
    "reason": "success",
    "result": {
        "SearchItemsCount": 2530,
        "SearchResultList": [
            {
                "spname": "【套装版】小米 红米Note 2 白色 移动4G手机 双卡双待",
                "sppic": "http://img1.wgimg.com/qqbuy/3633613617/item-000000000000000000000072D8948B31.0.jpg/200?56694D28",
                "spurl": "http://item.yixun.com/item-2229222.html",
                "spprice": "849.00",
                "className": "手机",
                "brandName": "小米",
                "siteName": "易迅网",
                "commentUrl": "",
                "commentCount": "1967",
                "TitleHighLighter": "【套装版】<font color=\"red\">小米</font> 红米Note 2 白色 移动4G手机 双卡双待",
                "ziying": "1",
                "siteid": "11",
                "id": "175342406"
            },
            {
                "spname": "小米4智能5吋大屏4G手机包邮Xiaomi/小米 小米手机4 2GB内存版16G",
                "sppic": "http://img.alicdn.com/bao/uploaded/i1/1714128138/TB2TB2FaVXXXXc4XXXXXXXXXXXX_!!1714128138.jpg_220x220.jpg",
                "spurl": "http://item.tmall.com/item.htm?id=42154201960",
                "spprice": "1299.00",
                "className": "手机",
                "brandName": "小米",
                "siteName": "天猫旗舰店",
                "commentUrl": "http://detail.tmall.com/item.htm?id=42154201960&is_b=1&cat_id=50024400&q=&rn=5e83fac4e85ca9d3794ae3cd102b6fb2&on_comment=1#J_TabBar",
                "commentCount": "209622",
                "TitleHighLighter": "<font color=\"red\">小米</font>4智能5吋大屏4G手机包邮Xiaomi/<font color=\"red\">小米</font> <font color=\"red\">小米</font>手机4 2GB内存版16G",
                "ziying": "1",
                "siteid": "278",
                "id": "197669012"
            }
        ]
    }
}
PHP示例<!--?php
// +----------------------------------------------------------------------
// | JuhePHP [ NO ZUO NO DIE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Juhedata <info@juhe.cn-->
// +----------------------------------------------------------------------
 
//----------------------------------
// 商品比价调用示例代码 - 聚合数据
// 在线接口文档:http://www.juhe.cn/docs/137
//----------------------------------
 
header('Content-type:text/html;charset=utf-8');
 
 
//配置您申请的appkey
$appkey = "*********************";
 
 
 
 
//************1.查询支持的商城信息************
$url = "http://api2.juheapi.com/mmb/allsites";
$params = array(
      "key" => $appkey,//应用APPKEY(应用详细页查询)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************2.比价简单查询接口************
$url = "http://api2.juheapi.com/mmb/search/simple";
$params = array(
      "keyword" => "",//要查询关键字
      "key" => $appkey,//应用APPKEY(应用详细页查询)
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
//************3.比价复杂查询接口************
$url = "http://api2.juheapi.com/mmb/search/complex";
$params = array(
      "keyword" => "",//搜索关键字
      "key" => $appkey,//应用APPKEY(应用详细页查询)
      "Site" => "",//商城编号,默认为0,即所有
      "PriceMin" => "",// 最低价格,0表示无最低价格限制
      "PriceMax" => "",//最高价格,0表示无最高价格限制
      "PageNum" => "",//页号,大于0
      "PageSize" => "",//每页返回结果数,上限为50
      "Orderby" => "",//排序规则,1、score 按权重从高到底排序,默认  2、price 按价格从底到高排序 3、sell 按销售从高到底排序
      "ZiYing" => "",//是否自营
      "ExtraParameter" => "",//0:结果包含淘宝数据,1:结果不包含淘宝数据
);
$paramstring = http_build_query($params);
$content = juhecurl($url,$paramstring);
$result = json_decode($content,true);
if($result){
    if($result['error_code']=='0'){
        print_r($result);
    }else{
        echo $result['error_code'].":".$result['reason'];
    }
}else{
    echo "请求失败";
}
//**************************************************
 
 
 
 
 
/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
 
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>