PHP cURL POST Jenkins job with parameters

蹲街弑〆低调 提交于 2019-12-12 03:37:17

问题


Triggering Jenkins job via following PHP script:

<?php

$testrun_id = "1744";
$cmd = "curl -X POST http://build:f9280f75396f83a0@mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode  json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
exec($cmd,$result);

?>

This script runs successfully on Mac and the jenkins job does get triggered. How do I make this script to work on Windows? I am getting following error when I run above PHP script on Windows?

curl is already installed on windows machine. Also, is there a better way to do cURL in PHP? Looking at this: http://php.net/manual/en/book.curl.php, can someone point me towards an example based on my curl command in the above PHP script(for Windows)? An example based on the curl command in my script would be ideal.


回答1:


you should check examples from here http://php.net/manual/en/curl.examples.php

Bellow is the code for you case,

$url = "http://build:f9280f75396f83a0@mobile-jenkins.domain.com:8080/job/android-test/buildWithParameters";     
$data = "POST_RESULTS=true&RUN_ID=".$testrun_id."&CHECK_NAME=SampleAutomatedPlan";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);    



回答2:


You need to set the content type for JSON

curl -H "Content-Type: application/json" -X POST http://build:f9280f75396f83a0@mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode  json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";

Just make sure you don't have any mix matched values.



来源:https://stackoverflow.com/questions/38157175/php-curl-post-jenkins-job-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!