【问题标题】:PHP cURL POST Jenkins job with parameters带参数的 PHP cURL POST Jenkins 作业
【发布时间】:2016-11-04 13:26:40
【问题描述】:

通过以下PHP 脚本触发Jenkins job

<?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);

?>

此脚本在 Mac 上成功运行,并且 jenkins 作业确实被触发。如何使此脚本在 Windows 上运行?在 Windows 上运行 PHP 脚本时出现以下错误?

curl is already installed on windows machine。另外,有没有更好的方法在 PHP 中做 cURL?看看这个:http://php.net/manual/en/book.curl.php,有人可以根据我在上述 PHP 脚本中的 curl 命令(对于 Windows)指出一个示例吗?我的脚本中基于 curl 命令的示例将是理想的。

【问题讨论】:

    标签: php windows curl jenkins php-curl


    【解决方案1】:

    你应该从这里查看示例http://php.net/manual/en/curl.examples.php

    下面是你的代码,

    $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);    
    

    【讨论】:

    • 不确定如何传递 curl 命令中的参数,例如 json=..parameters 和 --data-urlencode 参数
    • 这是一个带有 json 参数的单个调用。
    • 我为您的案例更新了我的答案,以使用 POST 传输数据,以及
    • 对于每个curl_setopt 行,我都会收到此错误:Warning: curl_setopt() expects parameter 1 to be resource, null given
    • nvm,我将所有curl_setopt 的第一个参数固定为ch 而不是curl,因为我们最初声明ch for curl init
    【解决方案2】:

    您需要为 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\"}]}'";
    

    只要确保您没有任何混合匹配值即可。

    【讨论】:

    • &lt;?php $testrun_id = "1744"; $cmd = "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\"}]}'"; exec($cmd,$result); ?&gt; 是否需要为 -H 参数转义任何字符?
    • 我试过这个:...-H \"Content-Type: application/json\",但它没有帮助,我得到了这个:stackoverflow.com/questions/33025656/…。另外,如果您查看上面的 dinesh 答案,我会收到Warning: curl_setopt() expects parameter 1 to be resource, null given
    猜你喜欢
    • 2016-12-21
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多