Pratik Jaiswal
Pratik Jaiswal

Reputation: 299

PHP cURL POST Jenkins job with parameters

Triggering Jenkins job via following PHP script:

<?php

$testrun_id = "1744";
$cmd = "curl -X POST http://build:[email protected]: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?

enter image description here

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.

Upvotes: 0

Views: 2008

Answers (2)

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

Bellow is the code for you case,

$url = "http://build:[email protected]: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);    

Upvotes: 2

unixmiah
unixmiah

Reputation: 3145

You need to set the content type for JSON

curl -H "Content-Type: application/json" -X POST http://build:[email protected]: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.

Upvotes: 0

Related Questions