-1

I am trying to use outreach API for my PHP website. On outreach, this is request they mentioned:

POST https://api.outreach.io/api/v2/prospects

{ "data": { "attributes": { "firstName": "Test_3", "lastName": "API" }, "type": "prospect" } }

In PHP , i converted this to following code but it gives error "Bad Request". Please help me what i am doing wrong, accessToken in below code is having correct value.

$urlCreateAccount = "https://api.outreach.io/api/v2/prospects";
$attributes = array("firstName"=> "Sally","lastName"=> "Biu");
$paramsCreateAccount = "type=prospect$attributes=".$attributes;
$headr[] = "Authorization:bearer $accessToken";

$curl = curl_init($urlCreateAccount);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $paramsCreateAccount);

$jsonResponseCreateAccount = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
farazch
  • 21
  • 3

2 Answers2

0

I can really say what is wrong in code. But try once this code. I have taken sample json data outreach api document

$jsonData = "{
  "data": {
    "type": "prospect",
    "attributes": {
      "emails": ["sally.smith@acme.example.com"],
      "firstName": "Sally",
      "title": "CEO"
    },
    "relationships": {
      "account": {
        "data": {
          "type": "account",
          "id": 1
        }
      }
    }
  }
}";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.outreach.io/api/v2/prospects",
  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 =>$jsonData,
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/vnd.api+json",
    "Authorization: Bearer <Access Token>",
    "Content-Type: text/plain"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Hope you will get your result. Also you can use postman to test the api first and from there you can take out the code fro the desired language

  • Thanks for your help. Below part is giving error, can you please point out what is wrong with your jsonData part: $jsonData = "{ "data": { "type": "prospect", "attributes": { "emails": ["sally.smith@acme.example.com"], "firstName": "Sally", "title": "CEO" }, "relationships": { "account": { "data": { "type": "account", "id": 1 } } } } }"; – farazch Apr 10 '20 at 05:49
  • The code for `$jsonData` has nested double quotes, try changing the outermost quotes to single quotes `$jsonData = '{` and `}';`. Ideally you should build the data as an array and then JSON encode the result, but try this first. – Nigel Ren Apr 10 '20 at 06:44
  • try out this one $jsonData = '{ "data": { "type": "prospect", "attributes": { "emails": ["sally.smith@acme.example.com"], "firstName": "Sally", "title": "CEO" }, "relationships": { "account": { "data": { "type": "account", "id": 1 } } } } }'; – Divyanshu Kumar Apr 10 '20 at 16:20
0

Its expecting JSON, so you need to POST JSON. Something like this...

$data = json_encode($data);
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $access,'Content-Type: application/json','Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
miknik
  • 5,748
  • 1
  • 10
  • 26