Search code examples
phpnullphp-curl

PHP APİ CURL Get Null


$curl      = curl_init();
curl_setopt_array($curl   , [

CURLOPT_RETURNTRANSFER =>TRUE,

CURLOPT_URL => 'https://ortam.etu.edu.tr/Services/get_user_card_information/?apikey=XXXXXXXXX&username=ZZZZZZZZZZ',

 ]);
$response = curl_exec($curl);
$response = json_decode($response, TRUE);

var_dump($response);

I was able to get a response from a different API call, but I cannot get a response from this API, it comes back as NULL.

https://api.nasa.gov/neo/rest/v1/feed?start_date=2023-12-01&end_date=2023-12-08&api_key=XXXXXXXXXXXXXXX This API works correctly. ​


Solution

  • You need to check for any errors in the cURL request using curl_error function which returns any error happens in the request phase:

    $error = curl_error($curl);
    var_dump($error); // Output: string(63) "SSL certificate problem: unable to get local issuer certificate"
    

    That's means that there is an issue with the SSL certificate on the requested endpoint.

    To solve this, you need to disable the SSL verification using

    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    

    or to request the UN-SECURED http like:

    CURLOPT_URL => 'http://ortam.etu.edu.tr/Services/get_user_card_information/?apikey=ZZZZZ&username=XXXX',
    

    Note the http://ortam. instead of https://ortam.

    However, this is not a good idea at all to disable the SSL verification.