Tripodkid Posted February 7, 2023 Share Posted February 7, 2023 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 😀! Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/ Share on other sites More sharing options...
requinix Posted February 7, 2023 Share Posted February 7, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605476 Share on other sites More sharing options...
Tripodkid Posted February 7, 2023 Author Share Posted February 7, 2023 (edited) 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 February 7, 2023 by requinix sigh Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605480 Share on other sites More sharing options...
requinix Posted February 7, 2023 Share Posted February 7, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605485 Share on other sites More sharing options...
Tripodkid Posted February 10, 2023 Author Share Posted February 10, 2023 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605550 Share on other sites More sharing options...
requinix Posted February 10, 2023 Share Posted February 10, 2023 What's the value in $response? Maybe it's some HTML page? Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605553 Share on other sites More sharing options...
Tripodkid Posted February 10, 2023 Author Share Posted February 10, 2023 (edited) 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 February 10, 2023 by Tripodkid Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605554 Share on other sites More sharing options...
kicken Posted February 11, 2023 Share Posted February 11, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605557 Share on other sites More sharing options...
Tripodkid Posted February 11, 2023 Author Share Posted February 11, 2023 Thanks so much kicken! When I use 49 minutes ago, kicken said: echo htmlspecialchars($response); it gives me html code but not the JSON survey data I'm looking for. I really appreciate your help and input! Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605558 Share on other sites More sharing options...
kicken Posted February 11, 2023 Share Posted February 11, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605559 Share on other sites More sharing options...
Tripodkid Posted February 11, 2023 Author Share Posted February 11, 2023 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! Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605570 Share on other sites More sharing options...
kicken Posted February 12, 2023 Share Posted February 12, 2023 18 hours ago, Tripodkid said: What am I missing here 🤔? Again, from the docs you linked: Quote All API access is over HTTPS to the api.getfeedback.com domain. Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605587 Share on other sites More sharing options...
Solution Tripodkid Posted February 12, 2023 Author Solution Share Posted February 12, 2023 But I'm using https, no? curl_setopt($ch, CURLOPT_URL, "https://app.getfeedback.com/surveys/1093727/responses"); Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605589 Share on other sites More sharing options...
Tripodkid Posted February 12, 2023 Author Share Posted February 12, 2023 Bingo! Thanks so much for your help! Originally my code was api but then the vender sent me this screen shot, hence app! Quote Link to comment https://forums.phpfreaks.com/topic/315891-help-using-php-to-get-json-data/#findComment-1605591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.