1

i have problem with one task - "your task will be to unhardcode photo getter by applying connection to Example Api. It's a simple JSON API with only one endpoint: http://exampleapi.com/

It is protected by API key. To be authorized you must pass api_key GET parameter to the endpoint. The key that you may use is 45adfxcvpas4aw4asd870cz34876azx6

As a response for request to this API you will get simple JSON object with just one attribute called "url", eg. {"url": "http://randombuilding123.jpg"}.

I write this function but its still sends me to 404 page. What im doing wrong Code :

public function getUrl(): string
{
    $url= 'http://exampleapi.com/';
    $ch = curl_init($url);
    $apikey= '45adfxcvpas4aw4asd870cz34876azx6';
    $headers = array(
        'Authorization: '.$apikey
    );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    if(!$response){
        die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
    }
    curl_close($ch);

    return '/images/404.jpg';`
ADyson
  • 57,178
  • 14
  • 51
  • 63
l4si0k
  • 11
  • 1
  • 404 usually means you got the URL wrong – ADyson Jul 15 '21 at 08:12
  • hmm , but could u tell me is my method looks good ? – l4si0k Jul 15 '21 at 08:32
  • Hang on actually, I just looked at the code again. what do you mean by "sends me to 404 page" exactly? You mean the curl request returns a 404? Or your own webserver returns 404 when you try to run the PHP? Obviously I don't know how you are using this getUrl() function, but at the end of it you wrote `return '/images/404.jpg`, and I'm not sure why you've done that, or what the calling code will do with it, but it doesn't seem to be relevant to the rest of the function. You are never using the actual response that you get from the curl request. – ADyson Jul 15 '21 at 08:35
  • As for whether the function "looks good", that's a meaningless question. The only way to tell if code is "good" for certain is to test it thoroughly against a set of pre-defined requirements – ADyson Jul 15 '21 at 08:37
  • by "sends me to 404 page" i mean that in case of any problems with API, internal public/images/404.jpg photo must be shown. – l4si0k Jul 15 '21 at 08:47
  • Ok, well in your code you're just sending them to that page in the case of success from the API. (If the curl call fails, your script will die before it gets there). And also that makes no sense as a requirement - 404 means "not found", it doesn't mean "the request failed". Try to return meaningful error messages. – ADyson Jul 15 '21 at 08:51
  • You need to properly examine the contents of $response to see if it contains the expected JSON – ADyson Jul 15 '21 at 08:53
  • json_encode($response) returns "403 - data are protected by law and API key – l4si0k Jul 15 '21 at 09:06
  • Ok. Well that's because you've sent the request incorrectly. Notice that the requirements said you had to provide the API key as a **GET parameter**, but you've sent it as an Authorization header. – ADyson Jul 15 '21 at 09:07
  • could u write me this? i dont know how to do it – l4si0k Jul 15 '21 at 09:13
  • You don't know what a URL parameter (sometimes called a GET parameter) is? Tell me, in this URL, which part is the parameter? `https://example.com/somePage.html?parameterName=parameterValue` . I'm pretty sure you'll have seen one of those before. – ADyson Jul 15 '21 at 09:32
  • thanks now $response returns "{\"url\":\"http:\\\/\\\/http://exampleapi.com\\\/images\\\/building1.jpg\"}" – l4si0k Jul 15 '21 at 09:56
  • Good so it's working. The only reason you've got all those slashes is probably because you've run `json_encode` on a string which is already JSON (so it ends up double-encoded). Just `echo $response` directly to see the original value – ADyson Jul 15 '21 at 10:08
  • so now i have to do : Frontend should always show correct image. In case of any problems with API, internal public/images/404.jpg photo must be shown. its possible to return image instead link? – l4si0k Jul 15 '21 at 10:13
  • Not sure what you mean? As I said earlier, I can't see what you do with the result of the getUrl() function, but I'm guessing that whatever you return from there is used as the `src` value in an ` – ADyson Jul 15 '21 at 10:15
  • And if decoding the JSON fails, that probably indicates that the API request fails so then you should show the error picture. Although as I said before, it's a ridiculous requirement to show a 404 error when no such problem has actually occurred, it would just mislead or confuse the user into thinking they had made a different mistake. Really it should just display a generic "something went wrong with the API request" error, and log the $response in the background so you can investigate it. – ADyson Jul 15 '21 at 10:18
  • Ok. And? You need to do what I've just described in my last two comments. – ADyson Jul 15 '21 at 10:34
  • Yes because $response is a JSON string. Stop and _think_ about what you're doing. You already know that $response contains something like `{"url": "http://randombuilding123.jpg"}`. So clearly when you put $response into the img you get ` – ADyson Jul 15 '21 at 11:52
  • Try reading these articles: https://www.php.net/manual/en/function.json-decode.php and https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php – ADyson Jul 15 '21 at 11:55
  • That might be cases where the API call fails for some reason, or at least doesn't return a result. Again I [mentioned this already](https://stackoverflow.com/questions/68390076/get-method-to-api-php?noredirect=1#comment120870339_68390076) - you need to write code to handle that situation. Just check that the result of json_decode isn't `null` before proceeding. – ADyson Jul 15 '21 at 13:01

0 Answers0