Jump to content

Recommended Posts

Hi,

 

I am trying to use the following code to connect to an API and then display the data to screen, however, at present when running the code nothing at all is displayed and nothing happens. I'm fairly new to PHP and just cannot figure out what I'm doing wrong. Please help!

 

<?php
 
ini_set('display_errors',"1");
 
function curl_request(){
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: basic ' . base64_encode('#######'.'+'.'##########'.':'.'##########')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close ($ch);
 
 
$obj = json_decode($json);
 
print ($obj['summary']);
 
}
 
print curl_request(); 
 
?>
 
The data structure of what I am trying to pull back is as follows:
 
Ticket {
* summary(String(100))
recordType(Enum)
}

 

  

Link to comment
https://forums.phpfreaks.com/topic/302801-connect-to-api-using-json/
Share on other sites

Hi Requinix,

 

I'm unsure what the contents are as I can't view anything in the browser when I run the code. I have checked to ensure the URL and authorization details are correct.

 

The following link details what should be returned: https://developer.connectwise.com/manage/rest?a=service?a=Service&e=Tickets&o=GET.

Edited by WebGeekUK

Try running just this:

<?php
 
ini_set('display_errors',"1");

header('Content-Type: text/plain'); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://eu.myconnectwise.net/v2017_1/apis/3.0/services/tickets/13934");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: basic ' . base64_encode('#######'.'+'.'##########'.':'.'##########')));
curl_exec($ch);
curl_close ($ch);

?>

I have solved the issue with the 'Cannot modify header information' error, however, I am still no closer to understanding why this doesn't seem to be working...

 

This is the page where the api data is being called: http://www.bournetown.co.uk/bourne-events3.php. 

Edited by WebGeekUK

It doesn't even show any of the api data that is being called in the browser header console...

Of course not: the API call is happening on your server, not in the browser.

 

I have solved the issue with the 'Cannot modify header information' error, however, I am still no closer to understanding why this doesn't seem to be working...[/size]

So how about we return to what I was asking about before. That test script I posted - what do you get if you run that?

Right now I don't care about $json.

 

Listen to me, okay?

 

This code:

<?php
 
ini_set('display_errors',"1");

header('Content-Type: text/plain'); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://eu.myconnectwise.net/v2017_1/apis/3.0/services/tickets/13934");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: basic ' . base64_encode('#######'.'+'.'##########'.':'.'##########')));
curl_exec($ch);
curl_close ($ch);

?>
Put the right username and password in there, then run it.

 

What is the output? If there's sensitive information then redact it, but otherwise post exactly what you see.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.