0

This is my PHP code: (name of the file is getname.php)

<?php
$query = $_GET['p']; //supplied as getname.php?p=easy+is+his+name

$link = 'http://api.genius.com/search?access_token=MY_ACCESS_TOKEN&q='.$query;
$json = file_get_contents($link);
$obj = json_decode($json);

echo $obj->response->hits[0]->result->title;
?>

When I'm running this code, the error is like,

file_get_contents(http://api.genius.com/search?access_token=MY_ACCESS_TOKEN&q=easy is his name): failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported -- at line 5

And when I write the code as,

<?php
$link = 'http://api.genius.com/search?access_token=MY_ACCESS_TOKEN&q=easy+is+his+name';
$json = file_get_contents($link);
$obj = json_decode($json);

echo $obj->response->hits[0]->result->title;
?>

, the expected results are shown. Why is it so?

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • Where is `$obj` output? or share your `access_token` to see output. – Muhammad Hassaan Dec 25 '15 at 15:26
  • You're asking why you can't have spaces in URL's ? – adeneo Dec 25 '15 at 15:27
  • 1
    fyi: you can do `"url?q=" . urlencode($query);` – adeneo Dec 25 '15 at 15:28
  • Also, you could try to use curl instead. You can configure it more easily – Crecket Dec 25 '15 at 15:28
  • Possible duplicate of [Is a URL allowed to contain a space?](http://stackoverflow.com/questions/497908/is-a-url-allowed-to-contain-a-space) – adeneo Dec 25 '15 at 15:30
  • So now you have the URL `http://api.genius.com/search?access_token=MY_ACCESS_TOKEN&q=getname.php?p=easy+is+his+name`, which doesn't really seem right either ? – adeneo Dec 25 '15 at 15:31
  • @adeneo The name of the file is getname.php – N3R4ZZuRR0 Dec 25 '15 at 15:33
  • According to your edited code, you now have an URL with two questionmarks and other strangeness. It's as easy as supplying a valid URL, and you won't get 505 server errors back – adeneo Dec 25 '15 at 15:34
  • urlencode() is working well – N3R4ZZuRR0 Dec 25 '15 at 15:39
  • Genius.com has an odd API. Likely susceptible to injection attacks based on the URL format. (instead of getname.php, what if I requested getSecretInfo.php). It could still be secure, but a reason to keep standards in mind for API's. – Kyle Wiering Dec 25 '15 at 15:52
  • Possible duplicate of [HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported error](http://stackoverflow.com/questions/2757404/http-request-failed-http-1-1-505-http-version-not-supported-error) – Evan Dec 25 '15 at 16:17
  • @KyleWiering thanks for the suggestion. – N3R4ZZuRR0 Dec 25 '15 at 16:25

1 Answers1

0

Corrected:

<?php
$query = $_GET['p']; //supplied as getname.php?p=easy+is+his+name

$link = 'http://api.genius.com/search?access_token=MY_ACCESS_TOKEN&q='.urlencode($query);
$json = file_get_contents($link);
$obj = json_decode($json);

echo $obj->response->hits[0]->result->title;
?>
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32