简体   繁体   中英

JIRA Update custom fields using REST, PHP and cURL

I am trying to update some custom fields using the REST API and PHP/cURL.

I'm wondering if I might have edited something without realizing it, while what I have below "worked" yesterday (I think), it does not work now.

I get varying responses using the different "methods", from:

  1. I get this one using the POST method, as it is uncommented below.

    HTTP 405 - The specified HTTP method is not allowed for the requested resource ().

  2. I get this one if I use the commented-out PUT method, with POST commented out.

     {"status-code":500,"message":"Read timed out"} 
  3. And this one mixing and matching PUT and POST.

     {"errorMessages":["No content to map to Object due to end of input"]} 

What am I missing/doing wrong? I am using the following code:

<?php

$username = 'username';
$password = 'password';

$url = "https://example.com/rest/api/2/issue/PROJ-827";
$ch = curl_init();

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

$test = "This is the content of the custom field.";

$data = <<<JSON
{
    "fields": {
        "customfield_11334" : ["$test"]
    }
}
JSON;

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Also tried, with the above two lines commented out...
// curl_setopt($ch, CURLOPT_PUT, 1);
// curl_setopt($ch, CURLOPT_INFILE, $data);
// curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($ch);
$ch_error = curl_error($ch);

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

curl_close($ch);

?> 

The problem here is that PHP's cURL API is not particularly intuitive.

You might think that because a POST request body is sent using the following option that a PUT request would be done the same way:

// works for sending a POST request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// DOES NOT work to send a PUT request
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_PUTFIELDS, $data);

Instead, to send a PUT request (with associated body data), you need the following:

// The correct way to send a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Note that even though you're sending a PUT request, you still have to use the CURLOPT_POSTFIELDS option to send your PUT request body. It's a confusing and inconsistent process, but it's what you've got if you want to use the PHP cURL bindings.

According to the relevant manual entry docs , the CURLOPT_PUT option seems to only work for PUTting a file directly:

TRUE to HTTP PUT a file. The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.

A better option IMHO is to use a custom stream wrapper for HTTP client operations. This carries the added benefit of not making your application reliant on the underlying libcurl library. Such an implementation is beyond the scope of this question, though. Google is your friend if you're interested in developing a stream wrapper solution.

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