1

I have never used Curl but I am trying to complete a self api project at my job just to gain some experience. And I am stuck on the first step... I want to authenticate with an api. So I am running this code and I expect to see a Success 200 response with my access token, etc but I get nothing. No error, no feedback, the page just opens up blank I have tired to use CURLINFO_HEADER_OUT from this page What Steps do you Take to Troubleshoot Problems with PHP cURL? but still I got a blank page
Anyway thank you to anyone in advantage for some tips

<?php
const TOKEN_ENDPOINT  = 'xxxxxx';
const GRANT_TYPE      = 'xxxxx';
const CLIENTID        = 'xxxxxxxxxxx';
const CLIENTSECRET    = 'xxxxxxxxxxxxxx';
const USERNAME        = 'xxxxxxxxxxxxxxxxx';
const PASSWORD        = 'xxxxxxxxxxxxxxx';

$clientCredentials = base64_encode(CLIENTID . ':' . CLIENTSECRET);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => TOKEN_ENDPOINT,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'grant_type=' . GRANT_TYPE . '&username=' . USERNAME . '&password=' . PASSWORD ,
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Accept: application/json',
    'Authorization: Basic ' . $clientCredentials
  ),
));


$response = curl_exec($curl);
curl_close($curl);
echo $response ;
?>

8
  • What's the HTTP status code? Commented Aug 16, 2022 at 17:18
  • No, that would be the payload of the response... try $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE); after curl_exec() Commented Aug 16, 2022 at 17:33
  • curl_getinfo() is your friend. Commented Aug 16, 2022 at 17:33
  • I checked F12 and found under the network tab, the HTTP status code 200 but to my server which is where this page lives. I don't see anything related to the api endpoints Commented Aug 16, 2022 at 17:35
  • 1
    You need to send the request as curl does... check out Postman (google it!), a tool with which you can make those calls very easily. Commented Aug 16, 2022 at 18:39

2 Answers 2

1

To check curl error, the best way is to use curl_error function

$response = curl_exec($curl);
if (curl_errno($curl)) {
    $error_msg = curl_error($curl);
}
curl_close($curl);
echo $response ;

See the description of libcurl error codes here

See the description of PHP curl_errno() function here

See the description of PHP curl_error() function here

Sign up to request clarification or add additional context in comments.

Comments

0

When all else fails, i do this (code snippet from my ApiHelper curl wrapper). Use your own logger or evidence printing mechanism. Most every time the answer to the puzzle is in the printed stuff :

        // we simply stream curlopt debug info to a temporary 
        // file, so we can log it out later (when helper is set
        // to verbose)
        $st       = microtime(true);
        $verbiage = null;
        if ($this->verbose) {
            // write out the curl debug stuff
            curl_setopt($ch , CURLINFO_HEADER_OUT , false);
            curl_setopt($ch , CURLOPT_VERBOSE , true);
            $verbiage = fopen('php://temp' , 'w+');
            curl_setopt($ch , CURLOPT_STDERR , $verbiage);
        }

        $resp                = curl_exec($ch);
        $end                 = microtime(true);           // get as float
        $delta               = 1000.0 * ($end - $st);    // treat as float
        $this->roundTripInMs = sprintf("%.2f" , $delta);
        $this->getInstanceLogger()->debug("WS call : round trip took " . sprintf("%.2f" , $delta) . " ms.");
     
        if ($this->verbose) {
            // rewind and log the verbose output
            rewind($verbiage);
            $verboseLog = stream_get_contents($verbiage);
            $this->getInstanceLogger()->info("Verbose cURL : \n$verboseLog");
            fclose($verbiage);
        }
        $this->curlinfo = curl_getinfo($ch);
        $this->verbose && $this->getInstanceLogger()->info("cURL info : \n" . json_encode($this->curlinfo , PEHR_PRETTY_JSON));
        $this->httpStatus = curl_getinfo($ch , CURLINFO_RESPONSE_CODE);
        if (!$resp) {
            if ($this->verbose) $this->getInstanceLogger()->error("CURL request to [$url] failed\n" . json_encode($this->curlinfo , PEHR_PRETTY_JSON));
            return 0;
        }
        curl_close($ch);
        if ($resp && $this->verbose) $this->getInstanceLogger()->debug("Received json \n" . $resp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.