phprestintercom

Intercom Rest API search contact (with PHP)


I'm trying to retrieve a contact in Intercom through their REST API. I'm using the following code but I get the following error :

CODE

        try {

            $postdataarray['query'] = [ "field" => "email","operator" => "=","value" => $value['email']];
            $postData = json_encode($postdataarray);
            $postHeader = [
                'Authorization:Bearer ergergergergergergergerg',
                'Accept: application/json',
                'Content-Type: application/json -d'
            ];
            $curl = curl_init("https://api.intercom.io/contacts/search/");
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, FALSE);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $postHeader);
            $content = curl_exec($curl);
            curl_close($curl);
            echo $content;

        } catch (Exception $e) {

            error_log($e->getMessage());
            
        }

ERROR

{ "errors": [ { "code": "not_found", "message": "The requested resource does not exist; check your path and try again" } ], "type": "error.list" }

UPDATE Now my code looks like this :

    try {

        $postdataarray['query'] = [ "field" => "email","operator" => "=","value" => $value['email']];
        $postData = json_encode($postdataarray);
        $postHeader = [
            'Authorization:Bearer zefzefzefzefzef',
            'Accept: application/json',
            'Intercom-Version: 2.3',
            'Content-Type: application/json -d'
        ];
        $curl = curl_init("https://api.intercom.io/contacts/search");
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $postHeader);
        $content = curl_exec($curl);
        var_dump ($content);
        curl_close($curl);
      

    } catch (Exception $e) {

        error_log($e->getMessage());

    }

Still doesn't work


Solution

  • you are sending a post request to retrieve a contact you should be using get method

    change this line:

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    

    to

    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");