php 我试图从www.example.com获取软件包的最新版本search.maven.org,但出现错误

vnzz0bqm  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(76)

“错误:无法从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;
    }
    }
?>
ee7vknir

ee7vknir1#

添加以下内容:

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

如果你使用curl和默认的user-agent,maven会返回403 forbidden。

相关问题