需要帮助以从网站获取JSON数据

时间:2019-07-03 08:46:41

标签: json

我需要从以下网站获取JSON数据:https://shopee.co.id/search?keyword=western%20digital

我已经尝试过并获得以下结果:https://shopee.co.id/api/v2/search_items/?by=relevancy&keyword=western%20digital&limit=50&newest=0&order=desc&page_type=search

没关系,直到我注意到价格为空并且需要该价格值

还有其他方法吗?也许我做错了

到目前为止的代码:

<?php

function http_request($url){

    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, $url);

    // set user agent    
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    // return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    //close curl 
    curl_close($ch); 

    echo $output;

    // return output
    return $output;
}

if (isset($_POST['find']))
    {
        $search = $_POST['text_value'];

        $search = str_replace(' ', '%20', $search);

        $url = "https://shopee.co.id/api/v2/search_items/?by=relevancy&keyword=western%20digital&limit=50&newest=0&order=desc&page_type=search";

        $profile = http_request($url);

        // JSON to Array
        $profile = json_decode($profile, TRUE);

        $i = 0;
        foreach($profile["data"] as $profil){
            //add name, price from json
        }
    }



?>

<!DOCTYPE html>
<html>
<head>
    <title>Curl Data JSON</title>
</head>

<body>
<form action = "index2.php" method = "POST">
    <input type = "text" name="text_value"><input type = "submit" value = "search" name = "find">
</form>

</body>
</html>

0 个答案:

没有答案