简体   繁体   English

在php中使用cURL在JIRA中为问题添加评论

[英]Using cURL in php to add a comment to an issue in JIRA

Inspired by using CURL in PHP to add an issue in JIRA via REST API , I tried to add a comment to an issue in JIRA, but I am getting 405 Error PHP中使用CURL通过REST API在JIRA中添加问题的启发,我试图在JIRA中为问题添加评论,但出现405错误

Error: Notice: Use of undefined constant CURL_HEADER - assumed 'CURL_HEADER' Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values HTTP Status 405 - Method Not Allowed type Status report message Method Not Allowed description The specified HTTP method is not allowed for the requested resource (Method Not Allowed). 错误: 注意:使用未定义的常量CURL_HEADER-假定为'CURL_HEADER'警告:curl_setopt_array():数组键必须为CURLOPT常数或等效的整数值HTTP状态405-不允许的方法类型状态报告消息方法不允许的说明指定的HTTP方法是不允许用于请求的资源(不允许使用方法)。

My code: 我的代码:

<?php  
$handle=curl_init();
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

$data = <<<JSON
{
    "body": "Please Ignore. Test Comment"
}
JSON;
curl_setopt_array(
    $handle,
    array(
        CURLOPT_URL=>'https://path.to.Jira/rest/api/2/issue/issue-123',
        CURLOPT_POST=>true,
        //CURLOPT_HTTPGET =>true,
        CURLOPT_VERBOSE=>1,
        CURLOPT_POSTFIELDS=>$data,
        CURLOPT_SSL_VERIFYHOST=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0,
        CURLOPT_RETURNTRANSFER=>true,
        CURL_HEADER=>false,
        CURLOPT_HTTPHEADER=> $headers,
        CURLOPT_USERPWD=>"username:password"
        //CURLOPT_CUSTOMREQUEST=>"POST"
    )
);
$result=curl_exec($handle);
$ch_error = curl_error($handle);

if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $result;
}

curl_close($handle);
?>

What could be the possible solution to this error? 该错误的可能解决方法是什么?

Although Borislav's answer is directed at the "symptoms" of your issue, the simple answer to both the cURL errors the OP is experiencing is the fact that the constant: 尽管Borislav的答案是针对您问题的“症状”,但OP遇到的两个cURL错误的简单答案是以下事实:

CURL_HEADER was typed incorrectly... it should be: CURL_HEADER输入错误...应该是:

CURLOPT_HEADER . CURLOPT_HEADER

All cURL option constants have CURLOPT- as a prefix. 所有cURL选项常量都以CURLOPT-作为前缀。 See PHP manual - cURL Transfer options and scroll down for the constants. 请参阅PHP手册-cURL传输选项,并向下滚动以获得常量。

Explaining the error trail a bit further: 进一步解释错误线索:

The first error: 第一个错误:

Notice: Use of undefined constant CURL_HEADER - assumed 'CURL_HEADER' 注意:使用未定义的常量CURL_HEADER-假定为'CURL_HEADER'

means that PHP doesn't recognize CURL_HEADER , simply because it's a typo . 意味着PHP无法识别CURL_HEADER ,仅仅是因为它是一个错字

This has a domino effect onto the curl_setopt_array() function. 这对curl_setopt_array()函数具有多米诺效应。 Because the constant above was typed/spelled incorrectly, this entire function failed and returned false, as per PHP manual on curl_setopt_array() : "If an option could not be successfully set, FALSE is immediately returned, ignoring any future options in the options array" 因为上面的常数输入/拼写错误,这整个函数失败和curl_setopt_array返回false,按照PHP手册():“如果选项无法成功设置,FALSE立即返回,忽略选项阵列中的任何未来的选择“

Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values 警告:curl_setopt_array():数组键必须为CURLOPT常数或等效的整数值

Seeing how that function returned false, no cURL options were set. 看到该函数如何返回false,因此未设置cURL选项。 But your script continues to execute, without the required information going into the cURL's HTTP request... and BOOM! 但是您的脚本将继续执行,而所需信息不会进入cURL的HTTP请求中……还有BOOM!

HTTP Status 405 - Method Not Allowed; HTTP状态405-不允许使用的方法; The specified HTTP method is not allowed for the requested resource. 所请求的资源不允许使用指定的HTTP方法。

The JIRA API throws a 405 response at you, because the HTTP request didn't conform to its requirements. JIRA API向您抛出405响应,因为HTTP请求不符合其要求。

All because 3 letters were missing from the constant: "CURL OPT _HEADER" 这是因为常量中缺少3个字母:“ CURL OPT _HEADER”

This is why programming is fun :D 这就是为什么编程很有趣的原因:D

The: 的:

Notice: Use of undefined constant CURL_HEADER - assumed 'CURL_HEADER' 注意:使用未定义的常量CURL_HEADER-假定为'CURL_HEADER'

means that PHP doesn't recognize CURL_HEADER as a constant. 表示PHP无法将CURL_HEADER识别为常量。 Maybe your version is old? 也许您的版本旧了? Or you've mistyped it, or it doesn't exist. 或者您输错了它,或者它不存在。 The: 的:

Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values 警告:curl_setopt_array():数组键必须为CURLOPT常数或等效的整数值

means that you have a wrong structure of the parameter you pass to curl_set_opt($param1, $param2) and the: 表示传递给curl_set_opt($param1, $param2)的参数的结构错误curl_set_opt($param1, $param2)并且:

HTTP Status 405 .... Blah, Blah HTTP状态405 .... Blah,Blah

means exactly what is says: The server is returning HTTP 405 Method Not Allowed . 表示的含义恰恰是:服务器返回HTTP 405 Method Not Allowed

Furthermore pure cUrl will cause you a lot of headaches and if you're developing a full-stack application that connects to JIRA it will be hard. 此外,纯cUrl会让您头疼不已,如果您正在开发连接到JIRA的全栈应用程序,将会很困难。 I've personally done a full featured application that connects to (all) JIRA REST endpoints with cUrl and then with Guzzle which is a library based on cUrl. 我亲自完成了一个功能齐全的应用程序,该应用程序先使用cUrl,然后使用Guzzle(这是基于cUrl的库)连接到(所有)JIRA REST端点。 Believe me - it will be much more easier to develop and (more importantly) maintain. 相信我-它会更容易开发和(更重要的是)维护。

So if you have the option to change your approach I encourage you to switch to Guzzle now. 因此,如果您可以更改方法,我建议您现在切换到Guzzle。 ;) ;)

If you're using composer (which you should be doing ;) ), add the following to your composer.json file in the project root: 如果您正在使用composer (您应该这样做;)),则将以下内容添加到项目根目录中的composer.json文件中:

{
    "require": {
        "guzzle/guzzle": "~3.1.1"
    }
}

Then: 然后:

composer.phar install

PEAR install is also an option: 也可以选择安装PEAR:

pear -D auto_discover=1 install guzzlephp.org/pear/guzzle

And of course you can also just include the guzzle.phar . 当然,您也可以只包含guzzle.phar

Check the Guzzle docs for more information. 查看Guzzle文档 ,了解更多信息。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM