如何使用PHP和cURL在JFrog Artifactory中创建/更新键/值对?

mi7gmzs6  于 2023-06-04  发布在  PHP
关注(0)|答案(1)|浏览(633)

我正在使用PHP 8.2来读取我们的JFrog Artifactory API,这很好用。现在我需要createupdate工件上的一些 * 属性 *-所以如果工件不存在,则create它,如果工件存在,则update它。
例如,我可以用下面的代码读取特定工件的所有 properties

<?PHP

// The full URL for the specific artifact and its "properties"
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties";

$ch = curl_init($api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "X-JFrog-Art-Api: ". $artifactoryKey,
));

// Execute the request to retrieve existing properties
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

?>

这将转储如下内容:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

现在我想在properties中创建一个新的键/值对。例如,如果我想创建一个名为MyKey的新键,其值为False,那么我希望看到以下结果:

string(123) "{
    "properties" : {
        "ComponentName" : [ "TestComponent" ],
        "ContactEmail" : [ "me@nowhere.com" ],
        "ContactName" : [ "John Doe" ],
        "VersionNumber" : [ "1.0.0" ]
        "MyKey" : [ "False" ]
    },
    "uri" : "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0"
}"

我尝试了各种解决方案,如:

// Define the new key/value pair
$newProperty = array(
    'key' => "MyKey",
    'value' => "False"
);

// Prepare the JSON payload
$payload = json_encode(array("properties" => array($newProperty)));

// Initialize cURL to update the properties
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-JFrog-Art-Api: ' . $artifactoryKey
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Execute the request to update the properties
$response = curl_exec($ch);
echo "<pre>";
var_dump($response);

...还有各种各样的调整,但我不能让它工作。执行上述解决方案会给予此错误:

string(98) "{
    "errors" : [ {
        "status" : 400,
        "message" : "Properties value cannot be empty."
    } ]
}"

我的帐户 * 应该 * 有足够的访问权限来写/修改(我不是管理员,但被告知),但错误消息看起来也不像是与权限相关的,所以任何帮助都将不胜感激-我希望这可能是一个简单的修复,因为我没有太多处理Artifactory东西的经验:-)

fumotvh3

fumotvh31#

事实上,我自己也发现了这一点,这是一个愚蠢/简单的错误。在找到正确的帮助页面后,我很容易地解决了这个问题-我可能应该在一开始就投入更多的时间来找到这个:-)
让我先展示一下解决方案:

<?PHP

// Full URL for specific artifact and the property to create/update
$api = "https://mysrv/api/storage/TestProduct/TestComponent/1.0.0/?properties=MyKey=False";

// Initialize cURL to create/update the properties
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "X-JFrog-Art-Api: ". $artifactoryKey
));

// Execute the cURL request
$response = curl_exec($ch);

// Dump the result
echo "<pre>";
var_dump($response);

// Check if the request was successful
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($httpCode);

// Close the cURL session
curl_close($ch);

?>

这段代码将导致一个HTTP Status 204 (No Content),这等于成功,它将创建一个新的属性,名为MyKey,值为False。cURL PUT命令将用于 * 创建 * 它或 * 更新 * 值。
在这里找到它的帮助,https://jfrog.com/help/r/jfrog-rest-apis/set-item-properties

相关问题