Announcement

Collapse
No announcement yet.

Create Lead API Stopped Working with Error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create Lead API Stopped Working with Error

    My API was working fine until recently but now it can't create a lead for some unexplained reason (I tested it on 2 different servers and it returns same error)

    Warning: curl_getinfo(): supplied resource is not a valid cURL handle resource in /var/www/vhosts/mydomain/httpdocs/CRM/api2.php on line 179

    Fatal error: Uncaught Exception: EspoClient: Unknown Error in /var/www/vhosts/mydomain/httpdocs/CRM/api2.php:138 Stack trace: #0 /var/www/vhosts/mydomain/httpdocs/CRM/api2.php(25): EspoApiClient->request() #1 {main} thrown in /var/www/vhosts/mydomain/httpdocs/CRM/api2.php on line 138

    Could you please advise what could cause this error.

    I've done rebuild and clear cache.

    Thank you
    Last edited by battersea_puppy; 02-10-2020, 11:29 AM.

  • #2
    Can you show your curl code?

    Comment


    • #3
      This is original ESPO CRM EspoAPI Client code I'm using - no changes copied from the their documentation and it worked until recently - no changes were made to CRM itself.

      ////API CLASS-----------------------------------
      class EspoApiClient
      {
      private $url;

      private $userName;

      private $password;

      protected $urlPath = '/api/v1/';

      protected $userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36';

      private $lastCh;

      private $lastResponse;

      public function __construct($url = null, $userName = null, $password = null)
      {
      if (isset($url)) {
      $this->url = $url;
      }

      if (isset($userName)) {
      $this->userName = $userName;
      }

      if (isset($password)) {
      $this->password = $password;
      }
      }

      public function setUrl($url)
      {
      $this->url = $url;
      }

      public function setUserName($userName)
      {
      $this->userName = $userName;
      }

      public function setPassword($password)
      {
      $this->password = $password;
      }

      /**
      * Send request to EspoCRM
      *
      * @param string $method
      * @param string $action
      * @param array|null $data
      *
      * @return array | \Exception
      */
      public function request($method, $action, array $data = null)
      {
      $method = strtoupper($method);

      $this->checkParams();

      $this->lastResponse = null;
      $this->lastCh = null;

      $url = $this->normalizeUrl($action);

      $ch = curl_init($url);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      if ($this->userName) {
      curl_setopt($ch, CURLOPT_USERPWD, $this->userName.':'.$this->password);
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
      }
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
      curl_setopt($ch, CURLOPT_HEADER, true);

      if (isset($data)) {
      if ($method == 'GET') {
      curl_setopt($ch, CURLOPT_URL, $url. '?' . http_build_query($data));
      } else {
      $payload = json_encode($data);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($payload))
      );
      }
      }

      $this->lastResponse = curl_exec($ch);
      $this->lastCh = $ch;

      $parsedResponse = $this->parseResponce($this->lastResponse);

      if ($this->getResponseHttpCode() == 200 && !empty($parsedResponse['body'])) {
      curl_close($ch);
      return json_decode($parsedResponse['body'], true);
      }

      $header = $this->normalizeHeader($parsedResponse['header']);
      $errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : 'EspoClient: Unknown Error';

      curl_close($ch);
      throw new \Exception($errorMessage, $this->getResponseHttpCode());
      }

      public function getResponseContentType()
      {
      return $this->getInfo(CURLINFO_CONTENT_TYPE);
      }

      public function getResponseTotalTime()
      {
      return $this->getInfo(CURLINFO_TOTAL_TIME);
      }

      public function getResponseHttpCode()
      {
      return $this->getInfo(CURLINFO_HTTP_CODE);
      }

      protected function normalizeUrl($action)
      {
      return $this->url . $this->urlPath . $action;
      }

      protected function checkParams()
      {
      $paramList = [
      'url'
      ];

      foreach ($paramList as $name) {
      if (empty($this->$name)) {
      throw new \Exception('EspoClient: Parameter "'.$name.'" is not defined.');
      }
      }

      return true;
      }

      protected function getInfo($option)
      {
      if (isset($this->lastCh)) {
      return curl_getinfo($this->lastCh, $option);
      }
      }

      protected function parseResponce($response)
      {
      $headerSize = $this->getInfo(CURLINFO_HEADER_SIZE);

      return [
      'header' => trim( substr($response, 0, $headerSize) ),
      'body' => substr($response, $headerSize),
      ];
      }

      protected function normalizeHeader($header)
      {
      preg_match_all('/(.*): (.*)\r\n/', $header, $matches);

      $headerArray = array();
      foreach ($matches[1] as $index => $name) {
      if (isset($matches[2][$index])) {
      $headerArray[$name] = trim($matches[2][$index]);
      }
      }

      return $headerArray;
      }
      }

      Comment


      • #4
        I have the same error as you...

        Comment

        Working...
        X