Jump to content

Help using PHP to get JSON data


Go to solution Solved by Tripodkid,

Recommended Posts

Hi everyone! I could really use some help getting JSON data from a remote server using PHP. I am fairly familiar with PHP and often use it to pass form data to MySQL and other simple tasks but I've never used it to send headers or use cURL. Please could you send me a code example showing how to get the data so at least I have a starting point Because, even after extensive web searches and reading O'reilly's Intro to JSON, I still don't know where to begin😵.

I have the following info to make the connection:
api.myurl.com
GET surveys/:survey_id/responses HTTP/1.1
Authorization: Bearer xxx
Content-Type: application/json

Many Thx! Chris 😀!

Link to comment
https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/
Share on other sites

Sending a Content-Type with a GET request is incorrect so that's not right. And I'm not sure why you're reading books on JSON when your issues are with cURL and PHP.

Here is the starting point of the documentation for how to use cURL in PHP. Check a couple of the examples, and Google, for the basic way to use it. It's pretty simple for a request like the one you've shown.
What you'd need to do special is add a custom Authorization header to the request. That's done through curl_setopt() and one of the options in there.

Write some code, give it a try, and see how things go. If you have problems then post what you've written and describe what's happening when you try it.

Thanks requinix!

The documentation from the vendor states "GET /surveys/:survey_id/responses HTTP/1.1" Please see here https://www.getfeedback.com/api/v1/#overview I have sent them an email to confirm.

Here is my code that generates this error "500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong."

<?php
$headers = array(
    'GET /surveys/:survey_id/responses HTTP/1.1',
    'Authorization: Bearer TOKENHERE',
    'Content-Type: application/json'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.getfeedback.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "key=value");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

?>

Thanks again for your help!

Chris

Edited by requinix
sigh

1. The 500 error would be due to a syntax error. I think your indentation in that $headers array is wrong - like it has non-breaking spaces instead of regular spaces. You may have copied this code from somewhere? Delete the leading spaces and type them out yourself.
2. The list of headers should only contain the Authorization - not the GET because that's not a header, and like I said not the Content-Type either (I think their docs meant to use an Accept...)
3. This is a GET request. It does not POST anything so there shouldn't be any "POST" options.
4. You're going to want CURLOPT_RETURNTRANSFER at some point later. Without, what the API returns will go to your browser, which is handy for the moment but not what you'll need.

The other problem is the URL. You're supposed to put a value in for that ":survey_id". Then add that path to the api.getfeedback.com you have below and put the whole thing in for the CURLOPT_URL. It's the same value that you would put into your browser to test out the API (if not for the fact that the authentication wouldn't work).

And please, don't post your Bearer token again. Protect it like it were your bank account number.

Thanks requinix that helped a lot.

BTW the Bearer token posted is not mine but the one shown in the link I shared in my earlier post 😀!

The problem I have now is that my code is returning a NULL value. Any further help would be much appreciated!

<?php
$headers = array(
'Authorization: Bearer abc123',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.getfeedback.com/surveys/1093727/responses");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

var_dump(json_decode($response, true));

?>

Great question!

If I try to output the array by

foreach($response as $response){

echo $response;

}

or

print_r($response);

The entire page is redirected to a getfeedback.com login page.

But if I echo individual elements of the array I can see it is an HTML page like you suspected.

echo $response[0] = <

echo $response[1] = !

echo $response[2] = d

etc.

So may question is how do I access the entire array so I can start using the data?

Thanks!

 

Edited by Tripodkid
3 hours ago, Tripodkid said:

The entire page is redirected to a getfeedback.com login page

That is because you are getting back HTML that probably contains a redirect that the browser is following since you're outputting that HTML.

You can avoid the browser interpreting the HTML by either escaping it when you output it or setting a content type of text/plain for your output.

echo htmlspecialchars($response);

or

header('Content-type: text/plain');
echo $response;

Setting the content type only works if you haven't sent other output yet.

 

23 minutes ago, Tripodkid said:

it gives me html code but not the JSON survey data I'm looking for.

The API documentation you linked earlier says this:

Quote

The Accept HTTP header must always be sent to signify the correct resource representation. At this time only application/json is supported.

Since you're not including that header you're not getting JSON.

 

Thanks kicken!

I added 

'Accept: application/json'

to the header so my code is now

<?php
$headers = array(
'Accept: application/json',
'Authorization: Bearer abc123'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://app.getfeedback.com/surveys/1093727/responses");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

curl_close($ch);

var_dump(json_decode($response, true));
echo htmlspecialchars($response);

?>

But $response is still outputting html and var_dump is still outputting NULL, I guess because there is no JSON in $response. What am I missing here 🤔?

Thanks again for your help!

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.