0

So I have a cURL request that authenticates with the Pardot API and its either successful or comes back with a code.

So I have the following cURL request:

public function create_prospect(array $fields = [])
    {
        $access_token = get_option('pardot_api_key');
        if (empty($access_token)) {
            $access_token = $this->get_oauth_token();
        }
        if (empty($fields)) {
            return false;
        }
        foreach ($fields as $key => $value) {
            $keys[] = $key;
        }
        if (isset($keys)) {
            $keys = implode(",", $keys);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'https://pi.pardot.com/api/v5/objects/prospects?fields=' . $keys);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_ENCODING, '');
        curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
        curl_setopt($curl, CURLOPT_TIMEOUT, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(
            $fields,
            JSON_THROW_ON_ERROR
        ));
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            "Authorization: Bearer {$access_token}",
            "Pardot-Business-Unit-Id: " . FH_Pardot_Settings::get_settings()['business_unit_id'],
            "Content-Type: application/json",
        ]);
        $response = curl_exec($curl);
        if (!curl_errno($curl)) {

            $info = curl_getinfo($curl, CURLINFO_HTTP_CODE);

            $this->write_log('PHP cURL Error: ' . curl_error($curl));
            curl_close($ch);
            return [];
        }
        curl_close($curl);

        return $response;
    }

I misformatted the api_key on purpose, and I get the following $info = curl_getinfo($curl, CURLINFO_HTTP_CODE); response: int(401) which isn't what I want.

But, the $response comes back as a string from the API call that shows:

string(71) "{"code":184,"message":"access_token is invalid, unknown, or malformed"}".

How can I properly handle and get the code from the $response so that I can do an if statement later?

Sem
  • 55
  • 5
  • It's JSON, you can use `json_decode()` to parse it. – Barmar Jun 20 '22 at 23:08
  • 1
    Read this: [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) and then apply what you've learned. – ADyson Jun 20 '22 at 23:10

0 Answers0