API PHP implementation DELETE call

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hans
    Junior Member
    • Apr 2018
    • 5

    API PHP implementation DELETE call

    Hi there,

    I am using the PHP implementation from the documentation and i am stumbling on the delete call. It does not work and i guess the problem is that the CURLOPT_CUSTOMREQUEST is never set to DELETE, if i am not having a payload, which i do not really need. Also worth mentioning is that it is behaving like it is a READ call since the default method is GET. So i guess my URL-building (and the rest) is correct, since it shows the correct dataset.
  • Hans
    Junior Member
    • Apr 2018
    • 5

    #2
    Okay the big question is: Am I doing something wrong? Or is it actually the case that I am right?

    Comment

    • Hans
      Junior Member
      • Apr 2018
      • 5

      #3
      Maybe I should mention that my problem is/was in the request function. My solution is to take the CURLOPT_CUSTOMREQUEST assignment out of the if-tree and put it above to the other curlopt assignments

      before:
      PHP Code:
      public function request($method, $action, array $data = null)
      {
        ...
        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); //line in question
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/json',
              'Content-Length: ' . strlen($payload))
            );
          }
        }
        ...
      }
      //getting read as result 
      

      after:
      PHP Code:
      public function request($method, $action, array $data = null)
      {
        ...
        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);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //the line got moved here
      
        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_POSTFIELDS, $payload);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/json',
              'Content-Length: ' . strlen($payload))
            );
          }
        }
        ...
      }
      //doing what its supposed to do 
      

      But my initial questions still need answers, since this is only my workaround. Also if I was actually right, the documentation for the api php implementation has to be updated (not necessarily with my solution but with what you think is best).

      Comment

      • yuri
        Member
        • Mar 2014
        • 8624

        #4
        Hi,

        Your fix seems to be fine. We will fix the doc. Thanks.
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        Working...