PHP获取百度指数API接口

<?php
error_reporting(0);
ini_set('display_errors', 0);

//你的百度cookies代码
$cookie="BDUSS=*********";


$name=$_GET['keyword'];
if(empty($name)){
    $baidu['status']=201;
    $baidu['msg']="参数不能为空!";
    $baidu=json_encode($baidu);    
    print_r($baidu);exit;
}
$day=intval($_GET['day']);
if($day<=0){
    $day=1;
}
$url = 'https://index.baidu.com/insight/brand/queryBrandId'; // 目标URL
$jsonData = array('keyword' => $name); // 你的JSON数据
$jsonData = json_encode($jsonData); // 将数组编码为JSON
$data=curl($url,$jsonData);
$data=json_decode($data,true);


if($data['data'][0]['id']){
    $day=intval($day)-1;
    $url = 'https://index.baidu.com/insight/brand/queryBrandIndex'; // 目标URL
    $jsonData = array('entityId' => $data['data'][0]['id'],'recentDay'=>$day,'regionId'=>0); // 你的JSON数据
    $jsonData = json_encode($jsonData); // 将数组编码为JSON
    $data=curl($url,$jsonData);
    $data=json_decode($data,true);
    //print_r($data);exit;
    if($data['data']){
        $baidu['status']=0;
        foreach ($data['data'] as $k => $v) {
            $baidu['data'][$k]['date']=$v['statDate'];
            $baidu['data'][$k]['value']=$v['value'];
        }
        $baidu=json_encode($baidu);
    }
}else{
    $search_param = array(
        'area' => '0',
        'word' => '[[{"name": "'.$name.'", "wordType": 1}]]',
        'days' => $day
    );
    $search_url = 'https://index.baidu.com/api/SearchApi/index?';
    $search_url .= http_build_query($search_param);
    $response = baidu_curl($search_url);
    $response_json = json_decode($response, true);
    $encrypted_data = $response_json['data'];
    $uniqid = $encrypted_data['uniqid'];
    $ptbk_url = "http://index.baidu.com/Interface/ptbk?uniqid=$uniqid";
    $ptbk_response=baidu_curl($ptbk_url);
    $ptbk_json = json_decode($ptbk_response, true);
    $ptbk = $ptbk_json['data'];

    $start_date = strtotime($encrypted_data['userIndexes'][0]['all']['startDate']);
    $end_date = strtotime($encrypted_data['userIndexes'][0]['all']['endDate']);
    for ($date = $start_date; $date <= $end_date; $date = strtotime('+1 day', $date)) {
        $timestamp_list[] = date('Y-m-d', $date);
    }

    // $encrypted_data_wise = $encrypted_data['userIndexes'][0]['all']['data'];
    // $decrypted_data_wise = decrypt($ptbk, $encrypted_data_wise);

    $encrypted_data_all = $encrypted_data['userIndexes'][0]['all']['data'];
    $decrypted_data_all = array_map(function($data) {
        return fill_zero($data);
    }, explode(',', decrypt($ptbk, $encrypted_data_all)));

    //print_r($decrypted_data_all);exit;
    if($decrypted_data_all[0]!=''){
        $baidu['status']=0;
        foreach ($timestamp_list as $k => $v) {
            $baidu['data'][$k]['date']=$v;
            $baidu['data'][$k]['value']=$decrypted_data_all[$k];
        }
        $baidu=json_encode($baidu);
    }else{
        $baidu['status']=-1;
        $baidu['msg']="获取失败!";
        $baidu=json_encode($baidu);
    }
}

print_r($baidu);


function fill_zero($data) {
    if ($data === '') {
        return '';
    } else {
        return $data;
    }
}
function baidu_curl($url){
    global $cookie;
    $headers = array(
        'Accept: application/json, text/plain, */*',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Cipher-Text: 1672733200884_1672792253933_1NLQa+cgc5N2JSNoinHAaMmrDrPtwqHL6D2NHONACx//1P+9YXcg/erBma8ucj43shvH2VsAi3Dzlo9cFfqA3k/PmqixjXJEslJCwNzCzNCVHs+/y7su33mGAxAtFWXrl55rYxzEJNGi4xM6jb4UUibTrVbOl46gKWq/7PVKAIzRyrJbxQP9pKmxECIpO12JbXFrA3leOj8xDZk69P1O/tNU6lD8eMPylUrgCp5k89c9EAD+Q4lgHhsZpTktcKTzKSbrJ5/l0GYNxNS96gEpS/0BnesBc6X52rqE7K4fNzrxm5cfgwbCJx/2+1ayhkI2gUMNDabQ1dnR0hr/NyWxeh7nYvxqarQHsZ+cu3XCt5uEHE4aAPgcXTfDgMsCQOrtMfDGKuxX5PiMDzDODjxSn8cDFRnJ+RMvfPjIIfq2P4k=',
        'Connection: keep-alive',
        'Cookie: '.$cookie,
        'Host: index.baidu.com',
        'Referer: https://index.baidu.com/v2/main/index.html',
        'Sec-Fetch-Dest: empty',
        'Sec-Fetch-Mode: cors',
        'Sec-Fetch-Site: same-origin'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_CAINFO, 'baidu.com.pem'); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    $response = curl_exec($ch); 
    curl_close($ch);
    if ($response) {
        return $response;
    } else {
        return '';
    }
}

function curl($url,$jsonData)
{
    global $cookie;
    $ch = curl_init($url);

    // 设置cURL选项
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_COOKIE, $cookie); // 设置cookie
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, "baidu.com.pem");
    $response = curl_exec($ch); // 执行cURL会话
    curl_close($ch); // 关闭cURL会话
    if ($response) {
        return $response;
    } else {
        return '';
    }
}

function decrypt($ptbk, $encrypted_data) {
    if (!$ptbk) {
        return "";
    }
    $n = strlen($ptbk) / 2;
    $d = array_combine(str_split(substr($ptbk, 0, $n)), str_split(substr($ptbk, $n)));
    $decrypted_data = array();

    // Ensure $encrypted_data is an array
    if (!is_array($encrypted_data)) {
        // Convert encrypted data string to array
        $encrypted_data = str_split($encrypted_data);
    }

    foreach ($encrypted_data as $data) {
        if (isset($d[$data])) {
            $decrypted_data[] = $d[$data];
        } else {
            echo "Warning: Key '$data' not found in decryption dictionary.<br animation: fadeInUp 0.5s ease forwards; >";
        }
    }
    return implode('', $decrypted_data);
}
PHP获取百度指数API接口
https://www.byue.cc/index.php/archives/3/
本文作者 八月
发布时间 2024-05-18
许可协议 CC BY-NC-SA 4.0
发表新评论