PHP Jira Service Desk REST API - php

I am struggling to create a request on Jira Service Desk in PHP.
My code is :
public function reportIssue(Request $request) {
//post
//authenticate to Jira ...
//create request ...
//response ....
//do something afterwards ... post ...
$jdata = json_encode($request);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request,
CURLOPT_USERPWD => SERVICE_USERNAME . ':' . SERVICE_PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
I am getting empty response body, still like something is wrong.
Pardon me if my mistake is obvious.

Had the same issue. Here is my working curl config:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, SERVICE_USERNAME . ':' . SERVICE_PASSWORD);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json;charset=UTF-8",
'X-ExperimentalApi: opt-in'
)
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($jdata));
$response = curl_exec($curl);

Related

Post request empty response PHP curl

When trying to send a post request to the server, the response returns an empty array "{}". But there is an answer on postsman. Presumably this is related to the content type of the response
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token?
grant_type=password&username={}&password={}";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: Basic token",
"Content-Type: application/json",
"Content-Length: 0",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
Usually the OAuth2 provider will want you to send the data inside the POST body and not in the URL. To do this you can send an array with your grant_type, username and password using CURLOPT_POSTFIELDS and remove the Content-Type and Content-Length headers since cURL will generate them for you when using CURLOPT_POSTFIELDS.
It should look something like this
<?php
$url = "https://api.business.kazanexpress.ru/api/oauth/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
'username' => '',
'password' => '',
'grant_type' => 'password'
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = [
"Authorization: Basic token",
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

Passing X-Api-Key through file_get_contents

I am trying to get data from a JSON endpoint on my streaming server. I've read that i need to pass an API key through the X-API-Key header. But not sure how to do this.
$url = file_get_contents('XXXXX/history');
$data1 = json_decode($url,true);
var_dump($data1);
this code works for me when getting json data from files that dont require the api key. How ever this now returns NULL as the key isn't being submitted through.
Any ideas?
try this:
$url = 'XXXXX/history';
$options = array('http' => array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$yourApiKey
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
try with CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'XXXXX/history');
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $yourApiKey
));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Atlassian Confluence Basic Login API NOT Working: Page Not Found

Working on Atlassian Confluence Basic Login API in PHP
$url = 'https://mysubdomain.atlassian.net/jira/rest/auth/1/session/';
$curl = curl_init();
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($username . ":" . $password)
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$postfields = array(
'username' => urlencode($username),
'password' => urlencode($password)
);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode( $postfields ));
$result = curl_exec($curl);
It show only Page not Found.
When I give wrong username or password, it showed authentication error.
What will be the correct code for Atlassian Confluence Basic Login through REST API.
Try using CURLOPT_USERPWD instead of the Authorization header.
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

PHP code on curl

THis code is not showing up the google home page. Please point out the error in it.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.com.kw");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>
Remember that if your response is string-based; curl automatically encodes the response in utf8. So it might be necessary to decode the String to get what you want. Otherwise, your code is OK but here is another variant:
<?php
$serviceURL = "https://www.google.com.kw";
$curl = curl_init();
$settings = array(
CURLOPT_URL => $serviceURL,
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $settings);
$response = curl_exec($curl);
$response = utf8_decode ( $response ); // <== DECODES THE UTF8 ENCODED STRING.
if(curl_errno($curl)){
var_dump( curl_error($curl) );
exit;
}
print($response);
curl_close($curl);
Try This :
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.co.in");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
curl_setopt($curl,CURLOPT_ENCODING, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($curl);
print $result;

Twilio cURL calling a phone

Based on the documentation of Twilio and Curl I have a php curl routine:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'.TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID.":".TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => urlencode( $CallURL ) ,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
It gives me an error:
{"code": 21213, "message": "No 'From' number is specified", "more_info": "https://www.twilio.com/docs/errors/21213", "status": 400}
I tried all the options, can anyone help?
I edited and added a "+" for the "From" number. But still the error remains the same.
Thanks in advance!
Twilio Developer Evangelist here.
Your code is really close. Just two small changes should resolve your issue. First you need to remove this line:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
This was truncating your POST data which resulted in the "From" information not being sent.
Second, you don't need to urlencode the $CallURL because curl handles this for us.
Once you make both of these changes your code should look like this and run without an error:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'. TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID .":". TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => $CallURL,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
Let me know if this gets your issues resolved.

Categories