sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 624 forks source link

Create contact API (https://api.sendgrid.com/v3/marketing/contacts) returns no response when called using PHP curl #1092

Open mww68 opened 2 years ago

mww68 commented 2 years ago

Issue Summary

Create contact API (https://api.sendgrid.com/v3/marketing/contacts) returns no response when called using PHP curl although the payload seems correct. No exception was thrown. Other APIs worked fine (e,g, GET https://api.sendgrid.com/v3/marketing/field_definitions, POST https://api.sendgrid.com/v3/marketing/contacts/search) using similar logic and set up. Thank you.

Steps to Reproduce

Here is the php code (the actual API key was removed):

Code Snippet


function create_contact() {

  $curl = curl_init();

  $url = "https://api.sendgrid.com/v3/marketing/contacts";

  curl_setopt($curl, CURLOPT_PUT, 1);

  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

  curl_setopt($curl, CURLINFO_HEADER_OUT, true);

  $apiKey = 'Bearer SG.MyAPIKey';
  curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    'Authorization: ' . $apiKey,
    'Expect:'
    ));

    $data = <<<DATA
    {
      "contacts": [{"email": "tp302.tp302@gmail.com"}]
    }
    DATA;

  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

  //for debug only!
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

  try {

    $resp = curl_exec($curl);
    $result = json_decode($resp);
    var_dump($result);

  } catch (exception $ex) {

    var_dump($ex);

  } finally {

      $info = curl_getinfo($curl);
      var_dump($info);

      curl_close($curl);
  }
}

Exception/Log

It ran for a while then returns null. Contact was not created in my SendGrid account (checked multiple times after wait).

# paste exception/log here
Here is the info returned from curl_getinfo():

array (size=38)
  'url' => string 'https://api.sendgrid.com/v3/marketing/contacts' (length=46)
  'content_type' => null
  'http_code' => int 0
  'header_size' => int 0
  'request_size' => int 221
  'filetime' => int -1
  'ssl_verify_result' => int 19
  'redirect_count' => int 0
  'total_time' => float 60.078775
  'namelookup_time' => float 0.034369
  'connect_time' => float 0.045485
  'pretransfer_time' => float 0.068833
  'size_upload' => float 0
  'size_download' => float 0
  'speed_download' => float 0
  'speed_upload' => float 0
  'download_content_length' => float -1
  'upload_content_length' => float 54
  'starttransfer_time' => float 0.068838
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '54.213.76.12' (length=12)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 443
  'local_ip' => string '192.168.1.133' (length=13)
  'local_port' => int 50342
  'http_version' => int 0
  'protocol' => int 2
  'ssl_verifyresult' => int 0
  'scheme' => string 'HTTPS' (length=5)
  'appconnect_time_us' => int 68734
  'connect_time_us' => int 45485
  'namelookup_time_us' => int 34369
  'pretransfer_time_us' => int 68833
  'redirect_time_us' => int 0
  'starttransfer_time_us' => int 68838
  'total_time_us' => int 60078775
  'request_header' => string 'PUT /v3/marketing/contacts HTTP/1.1
Host: api.sendgrid.com
Accept: */*
Content-Type: application/json

Technical details: