REST-ful connection using cURL header issues

I've spent a lot of time troubleshooting using PHP documentation, API documentation, and other posts on stackoverflow and finally asking for help.

I am trying to write an interface using the new pbSmartConnections API: API Documentation

I'm having trouble with fsockopen and cURL, however I seem to be able to get a more distant process using cURL, so that's what I'm presenting here. Here's the problem:

In my understanding of the documentation, I have to pass the ApiKey as part of the header. When I do this, no matter how I tried to structure the code, I ALWAYS get the following response:

{
    "ErrorCode": 10,
    "Message": "Unauthorized"
}

      

I hope an SO member can see something in my code below (please offer any criticisms and / or suggestions too!): (NOTE: The API key below is valid. It is connected to an account that has nothing useful, so feel free to use it in your testing)

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>

      

If you're curious, although I would like this to work as part of the header, I also tried passing it as part of the url:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.pbsyscontrol.com/v1/Ping?ApiKey=41460b3f-8f35-4878-b78d-49ca7f29c071");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type"=>"application/json", "Accept"=>"application/json", "ApiKey"=>"41460b3f-8f35-4878-b78d-49ca7f29c071"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
?>

      

+3


source to share


2 answers


I finally heard from support and what they pointed out was that I was using the wrong URL (although at this time it is the URL in their API Docs )

The URL in the API documentation was their STAGING, not PRODUCTION. Surprisingly, switching the url to the correct one they sent back is rest.api.pbsmartconnections.com

for the connection. This one change and it all started out fine.



THANKS to everyone who looked and @mvdnes for advice on customizing headers.

+2


source


The PHP documentation says:

Array of HTTP header fields to install in format array('Content-type: text/plain', 'Content-length: 100')

Thus, you want to use the following line instead of the original:



 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json", "ApiKey: 41460b3f-8f35-4878-b78d-49ca7f29c071"));

      

However, this does not solve the problem that the ApiKey is probably not valid.

+2


source







All Articles