0

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?

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.

Pratik Jaiswal
  • 309
  • 7
  • 26

2 Answers2

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: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);    
Pratik Jaiswal
  • 309
  • 7
  • 26
Create Explorer
  • 357
  • 2
  • 20
0

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.

unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • `` do I need to escape any character for -H param? – Pratik Jaiswal Jul 02 '16 at 06:49
  • I have tried this: `...-H \"Content-Type: application/json\"` but it did not help and I get this: http://stackoverflow.com/questions/33025656/nothing-was-submitted-error-from-jenkins-when-submitting-php-curl-to-build. Also if you check dinesh's answer above, I am getting `Warning: curl_setopt() expects parameter 1 to be resource, null given`. – Pratik Jaiswal Jul 02 '16 at 14:57