Rest API request with PHP curl not working

Chris Taylor January 29, 2019

If I run this curl command from the command line it works like a dream and I get a json array back

$ curl -u chris@christaylordeveloper.co.uk:my-api-token-here -X GET -H "Content-Type: application/json" https://christaylordeveloper.‍atlassian.‍net/rest/api/3/issue/KMCI-2/worklog

 When I run the equivalent command from php:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://christaylordeveloper.‍atlassian.‍net/rest/api/3/issue/KMCI-2/worklog');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_USERPWD, 'chris@christaylordeveloper.co.uk' . ':' . 'my-api-token-here');

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
}
curl_close ($ch);

I get the following error

Error: Could not resolve host: christaylordeveloper.???atlassian.???net

(including the question marks)

I have tried this PHP script from inside a Symfony app in Docker on my laptop and the same app on a GoDaddy server.

Could anyone explain what I'm doing wrong please?

1 answer

1 accepted

0 votes
Answer accepted
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 31, 2019

I'm not a PHP expert by any means, that said, your error message makes it appear as if additional characters are getting added to the the periods in the URL.  Why that is exactly, I'm not certain yet.

I would recommend trying to take a closer look at our REST API documentation for that endpoint of GET /rest/api/3/issue/{issueidOrKey}/worklog in that section there are examples of using curl, node.js, java, python, and php there.  From that PHP example there:

// This code sample uses the 'Unirest' library:
// http://unirest.io/php.html
$headers = array(
  'Accept' => 'application/json',
  'Bearer' => ''
);

$response = Unirest\Request::get(
  'https://your-domain.atlassian.net/rest/api/3/issue/{issueIdOrKey}/worklog',
  $headers
);

var_dump($response)

From this I gather that your php code is not using this unirest library that our examples site.   I suspect that you will need that library, or some other equivalent library, to help make these kinds of http requests using PHP.

Chris Taylor January 31, 2019

Thank you,

I was able to get this working with the lib you suggested, but not in quite the same way.  I think it is because I'm not using a 'Bearer' token.  The token I am using is on page https://id.atlassian.com/manage/api-tokens perhaps this is not a bearer token?

How do I get a bearer token?  Then perhaps I can use the code as set out in your examples, exactly, which would be better.

I had to use this instead, with basic auth:

$headers = array( 'Accept' => 'application/json' );

// basic auth
\Unirest\Request::auth( 'chris@christaylordeveloper.co.uk', 'my-api-token' );

$response = \Unirest\Request::get(
'https://christaylordeveloper.atlassian.net/rest/api/3/issue/KMCI-2/worklog',
$headers
);

// this works for me
var_dump($response);
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 1, 2019

Glad to hear that part at least worked.  There is another guide that explains how you can use that API token along with your username and then encode that string from the format of

username:API_Token

into a base64 string.  When you do that, you can use a slightly different header for authorization.  More details on using this in https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/

Our example there is using curl when explaining this, so I'm not exactly sure of that syntax for php, but I suspect that this could be your Bearer parameter in that specific header.  This is just an alternative way of using basic auth.

Chris Taylor February 1, 2019

Very happy to accept this answer and thank you.  Would be good to know what a 'Bearer' token is however because I can't get the example in the Jira documentation to work.  I can now get my worklogs however, which is fine.

Suggest an answer

Log in or Sign up to answer