PHP:JSON Api男高音

时间:2017-08-05 10:00:29

标签: php json

如何从https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8results->media->nanomp4->url

获取所有网址
if($json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8")) {
    $obj = json_decode($json);
    echo $obj->results->{"media"}->{"nanomp4"}->{"url"};
}

Function cURLGetContents($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

2 个答案:

答案 0 :(得分:0)

On a circulate basis:

$json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8");
$urlList = getAllUrls((array) json_decode($json, true));

function getAllUrls($input, $list = []) {    
    foreach ($input as $key => $data) {
        if (is_array($data)) {
            $list = getAllUrls($data, $list);
        } elseif ($key === 'url') {
            $list[] = $data;
        }
    }
    return $list;
}

答案 1 :(得分:0)

$endereco = "https://api.tenor.com/v1/search?key=MBDPHCT6LA4H&q=sexo&limit=2";    
$GrabURL = cURLGetContents($endereco);    
$searchResponse = json_decode($GrabURL, true);    
foreach ($searchResponse["results"] as $searchResult) {    
    print_r($searchResult["media"][0]["tinygif"]["url"]);       
} 

function cURLGetContents($url) {
    $ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL, $url);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
    $data = curl_exec($ch);    
    curl_close($ch);    
    return $data;   
}