GET方法的JSON数据

时间:2019-04-30 09:24:11

标签: json get

如何从API获取.json文件中的json数据以获取产品查询?

示例:https://api.abcd.com/v1/products/search.json?q=ball

header[key=abc, value=xyz] 

从获取查询中像邮递员一样获取json数据

{
    "Results": [
        {
            "Id": 5481192,
            "Name": " Shirt",
            "Description": " tweens. 100% Cotton.",
            "ShortDescription": " Raglan Shirt",
            "Number": "4253121",
            "ImageUrl": "media/295428",
            "VirtualSampleImages": [
                {
                    "Id": 2245428,
                    "ImageUrl": "virtualsample/2529548"
                }
            ],
            "ConfigId": "23",
            "Supplier": {
                "Id": 613,
                "Name": "a",
                "AsiNumber": "3529721",
                "Phone": {
                    "Work": "(451) 488-0417",
                    "$index": 1
                }
            },
            "Price": {
                "Quantity": 11,
                "Price": 133.7,
                "Cost": 61.85,
                "DiscountCode": "P",
                "CurrencyCode": "USD"
            },
            "IsNew": false,
            "IsConfirmed": false,
            "HasVirtualSample": true
        }
    ],
    "Selections": {},
    "Dimensions": {},
    "Links": {
        "Self": "?q=shirts&page=1&rpp=1",
        "Next": "?q=shirts&page=2&rpp=1"
    },
    "Query": "shirts",
    "Breadcrumb": "\"shirts\"",
    "Page": 1,
    "ResultsPerPage": 21,
    "ResultsTotal": 78,
    "SuppliersTotal": 6677,
    "CompletedIn": 7
} 

我想将json数据保存到.json文件中,然后自动保存到具有单独列的MySQL数据库中。

2 个答案:

答案 0 :(得分:0)

您可以简单地使用curl进行保存到文件的过程。

curl 'https://api.abcd.com/v1/products/search.json?q=ball' -H 'Accept: application/json, text/plain, */*' -H 'key: abc' -H 'value: xyz' -o dump.json

然后,您可以使用LOAD_FILE将此文件加载到表中:

INSERT INTO table_name (STAMP,file_content) VALUES(NOW(),LOAD_FILE("dump.json"));

整个过程取决于您的脚本/编程语言。您能告诉我们更多有关您的技术背景吗?

答案 1 :(得分:0)

$ curl = curl_init();

$ options = array(CURLOPT_URL =>“ http://api.abc.com/v1/products/search.json?q=ball”,CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING =>"",CURLOPT_FOLLOWLOCATION =>true,CURLOPT_MAXREDIRS => 10,CURLOPT_TIMEOUT=>30,CURLOPT_HTTP_VERSION=>CURL_HTTP_VERSION_1_0,

CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_POSTFIELDS=>"", 

CURLOPT_HTTPHEADER=> array("authorization: AsiMemberAuth client_id=50041351&client_secret=55700485cc39f1",

"cache-control: no-cache"), CURLOPT_HEADER=> true);


curl_setopt_array($curl, $options);

$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

if ($err)

  {

            echo "cURL Error #:" . $err;

  } 

    else

  {

    echo $response;

  }