首頁  >  問答  >  主體

嘗試從 search.maven.org 檢索最新版本的套件時遇到錯誤

「錯誤:無法從 Maven 儲存庫擷取資料」

同時從 search.maven.org 檢索最新版本號。 編寫了以下 php 程式碼,我將針對所有其他套件進行修改。

<?php
    $url = "https://search.maven.org/solrsearch/select?q=a:angus-activation-project&rows=1&wt=json";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);

    if ($response === false) {
        echo "Error: Retrieving data from the Maven repository failed";
    } else {
        $data = json_decode($response);
    if ($data === null || !property_exists($data, 'response') || count($data->response->docs) == 0) {
        echo "Error: Failed to retrieve the latest version of the artifact from the Maven repository";
    } else {
        $latestVersion = $data->response->docs[0]->v;
        echo "Latest version is" . $latestVersion;
    }
    }
?>

P粉461599845P粉461599845170 天前1456

全部回覆(1)我來回復

  • P粉409742142

    P粉4097421422024-04-06 09:09:10

    新增此:

    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109.0");

    如果您使用預設使用者代理程式的curl,maven會回傳403禁止。這樣你的請求看起來像 Firefox

    回覆
    0
  • 取消回覆