简体   繁体   中英

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

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).

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:

CURL_HEADER was typed incorrectly... it should be:

CURLOPT_HEADER .

All cURL option constants have CURLOPT- as a prefix. See PHP manual - cURL Transfer options and scroll down for the constants.

Explaining the error trail a bit further:

The first error:

Notice: Use of undefined constant CURL_HEADER - assumed 'CURL_HEADER'

means that PHP doesn't recognize CURL_HEADER , simply because it's a typo .

This has a domino effect onto the curl_setopt_array() function. 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"

Warning: curl_setopt_array(): Array keys must be CURLOPT constants or equivalent integer values

Seeing how that function returned false, no cURL options were set. But your script continues to execute, without the required information going into the cURL's HTTP request... and BOOM!

HTTP Status 405 - Method Not Allowed; The specified HTTP method is not allowed for the requested resource.

The JIRA API throws a 405 response at you, because the HTTP request didn't conform to its requirements.

All because 3 letters were missing from the constant: "CURL _HEADER" _HEADER”

This is why programming is fun

The:

Notice: Use of undefined constant CURL_HEADER - assumed 'CURL_HEADER'

means that PHP doesn't recognize CURL_HEADER as a constant. 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

means that you have a wrong structure of the parameter you pass to curl_set_opt($param1, $param2) and the:

HTTP Status 405 .... Blah, Blah

means exactly what is says: The server is returning 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. 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. 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. ;)

If you're using composer (which you should be doing ;) ), add the following to your composer.json file in the project root:

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

Then:

composer.phar install

PEAR install is also an option:

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

And of course you can also just include the guzzle.phar .

Check the Guzzle docs for more information.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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