簡體   English   中英

使用php7和curl通過api在jenkins中創建作業

[英]Using php7 and curl to create job in jenkins via api

我正在嘗試使用rest api和php7在jenkins中創建工作
95%的人確定我需要這樣的請求

curl -s -XPOST 'http://jenkins/job/FolderName/createItem?name=yourJobName' --data-binary @config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken

但是我總是會收到諸如“無法持久保存config.xml”和“在序言中不允許內容”之類的錯誤。 我的猜測是問題出現是因為我不使用--data-binary參數
我只是不知道如何在php 7中使用“ new CurlFile
為了理解,這是我的代碼:

$url = static::$host.'/job/'.$folderName.'/createItem?name='.$jobname;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, Array('file' => new CurlFile($xmlConfigurationFile)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, static::$user.":".static::$pass);
$headers = array('Content-Type: text/xml');
if ($jenkins->areCrumbsEnabled()){
    $headers[] = $jenkins->getCrumbHeader();
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);

在$ xmlConfigurationFile中是文件的路徑
auth,csrf,文件夾創建或文件路徑也沒有問題

所以問題是:
如何通過php7中的curl發送二進制的xml文件?

提前致謝

我已經解決了
我不懂邏輯,但對我有用
我已經改變了

curl_setopt($curl, CURLOPT_POSTFIELDS, Array('file' => new CurlFile($xmlConfigurationFile)));

其中$ xmlConfigurationFile是文件的路徑。 對此

curl_setopt($curl, CURLOPT_POSTFIELDS, $xmlConfiguration);

其中$ xmlConfiguration是文件內容
作業已創建

如果有人發布解釋會很酷,但是問題已解決

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM