0

I have this really long JSON string: http://pastebin.com/2jJKSGHs , which is being pulled from a music API.

I have this code set up to parse it ( http://pastebin.com/EuJtuhHg ):

$url = "https://api.discogs.com/database/search?type=artist&q=pink[keyandsecretredacted]"; 

//initialize the session
$ch = curl_init();

//Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_USERAGENT, 'YourSite/0.1 +http://your-site-here.com');

//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $url);

//Ask cURL to return the contents in a variable instead of simply echoing them
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the curl session
$output = curl_exec($ch);

//close the session
curl_close ($ch);


//decode and print output
$output = json_decode($output2, true);
echo($output['results'][0]['title']);

When I insert the contents of the JSON string directly into my code, the json_decode works perfectly on it. But when I try to grab it from the API using the method above, nothing prints on my page -- it's just empty. Printing out json_last_error returns "0", so it's not detecting any errors.

Any ideas why this might be happening?

m0a
  • 1,005
  • 2
  • 15
  • 29
  • 1
    I copied the contents of the raw pastebin to a `json` file, imported it to a variable, used `json_decode`, and it worked perfectly. There's something else going on in your code that is causing the error if `json_decode` didn't work for you. – Jon Jan 03 '16 at 23:09
  • 1
    You're right. I should have tested that before posting. It works great when I insert the text directly into the code. Since this text comes from an API, I'm using curl to grab it ( http://pastebin.com/EuJtuhHg ). /Then/ it doesn't work. It actually prints nothing to the page, and when I run json_last_error() it says there were no errors. I assumed the apostrophe was causing it for diff reasons, but now I wonder if it's something more. – m0a Jan 03 '16 at 23:22
  • 1
    Ahh, I'm glad I came back to look to see you changed what you were showing. There's no real 'error' in your code. you call `json_decode` on `$output2`, which contains nothing, you want to call it on `$output` and it works perfectly ^^ – Jon Jan 03 '16 at 23:33
  • 1
    can you var_export the $output of $output = curl_exec($ch); – Atilla Arda Açıkgöz Jan 03 '16 at 23:33
  • 1
    Also, either delete this question, or flag it so a mod can remove your api key and secret from the post history. – Jon Jan 03 '16 at 23:33
  • 1
    Actually, after seeing you posted that code to pastebin as a guest, should try and change your key and secret now... =] – Jon Jan 03 '16 at 23:40
  • Thank you so much for all the help! And I am changing my key and secret now, haha. ^^ Thanks again! – m0a Jan 04 '16 at 00:35

1 Answers1

1

Replace

$output = curl_exec($ch);

with

$output2 = curl_exec($ch);

Otherwise $output2 isn't defined, and json_decode is using an undefined variable:

$output = json_decode($output2, true);
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Thank you, but unfortunately it still hasn't caused the code to work. It still shows up blank when I try to echo or print_r the contents of $output. Also, if I use var_export($output) or var_dump($output) it returns false. – m0a Jan 03 '16 at 23:43
  • Don't you mean `$output2`? Please update your question with the newest code. – Martin Konecny Jan 03 '16 at 23:44
  • Works here - the output of `echo($output['results'][0]['title']);` is Pink Floyd. Looks like there is something wrong with your Curl - Are you on Linux/OS X/Windows? – Martin Konecny Jan 03 '16 at 23:52
  • I'm using Windows 8 and XAMPP -- maybe XAMPP is the problem? I have cURL enabled in the xampp/php/php.ini file . – m0a Jan 03 '16 at 23:57
  • Thank you. I ran phpinfo() and it does appear to be enabled and up to date. Maybe this is a problem with the API itself, but Discogs is popular. I will probably try to reach out on their forums. – m0a Jan 04 '16 at 00:02
  • 1
    Nope, I ran your original code against their API and it worked perfectly. What happens if you try something like `https://google.com`? – Martin Konecny Jan 04 '16 at 00:04
  • 1
    @matthew It's not the API, runs perfectly on my home system and my server and on PHPFiddle. It's your setup with `curl` in XAMPP ^^ – Jon Jan 04 '16 at 00:05
  • You mean setting $url = www.google.com and running echo($output2)? That seems to work as intended -- it makes my page look like a Google clone. @Jon I have never used phpFiddle before, but you're right, it works there! I am REALLY not sure what is going on with my setup, but now I can at least focus my efforts on that rather than the code itself! – m0a Jan 04 '16 at 00:10
  • 1
    @matthew try it with `https://google.com`. Judging from everything I've been able to see here and from comments, I would suspect it's how curl is treating your SSL connections (or PHP itself). Possible test with `file_get_contents` via https if doing it via curl doesn't work and if that doesn't work, but does for a non `https`, look in to the SSL connection specifically. I use OpenSSL for a lot of my projects, so what I had to do for that may be about the same requirements, but I'm not sure. It will give you a good direction though. – Jon Jan 04 '16 at 00:13
  • Wow, yes, www.google.com worked but `https://google.com` came up blank. `echo file_get_contents($url);` did work when url was set to `https://google.com` . I used `ini_set('user_agent', 'YourSite/0.1 +http://your-site-here.com');` and `$output3 = file_get_contents($url); $output3 = json_decode($output3, true); echo($output3['results'][0]['title']);` and finally "Pink Floyd" was gloriously printed on my site! =) Thanks so much. This is excellent. Is there a downside to doing it this way instead of cURL? I will research my SSL connection problems either way. So glad to see this running! – m0a Jan 04 '16 at 00:30
  • 2
    For debugging purposes *only*, try the following `curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);` (not recommended in production code!!). If it works with that line, it means that Curl is not setup with any certificates for some reason. I'd also recommend turning on all error reporting, so you can see if it's reporting something like "SSL certificate problem" – Martin Konecny Jan 04 '16 at 00:35
  • 1
    @matthew Yes, a lot of shared hosting servers will disallow you the ability to grab a web page with `file_get_contents` (or, for the most part, any of the `file` class of commands) – Jon Jan 04 '16 at 00:44