zinok / ZohoBooksApi

PHP API for Zoho Books REST API
MIT License
1 stars 3 forks source link

Any way to attach a file to an invoice? #5

Open mamont77 opened 7 years ago

mamont77 commented 7 years ago

Hello, Zinok.

Could you reply is possible and you have time? Any idea how to make request like

$attachment = base64_encode(
              file_get_contents(__DIR__ . $invoice_attachment)
            );

            $parameters = array(
              'can_send_in_mail' => TRUE,
              //'attachment' => $attachment,
            );

            try {
              $result = $zoho->makeApiRequest(
                'invoices/' . $invoice_id . '/attachment',
                'POST',
                $parameters,
                FALSE,
                TRUE
              );

              echo '<pre>';
              print_r($result);
              echo '</pre>';

            } catch (Exception $e) {
              logger('Zoho Exception', $zoho->lastRequest['dataRaw']);

            }

The "attachment" should be as multipart/form-data, however, "can_send_in_mail" as the parameter.

More details: https://www.zoho.com/books/api/v3/invoices/attachment/#add-attachment-to-an-invoice

zinok commented 7 years ago

Well, it could be done with a small change in the part of building parameters, where instead of json_encode you will need to pass structure prepared for curl for post of file. Unfortunately I do not have time at the moment to modify the class for this. But if you will send a patch I will gladly apply it.

mamont77 commented 7 years ago

Hello, Andrii. Thank you very much. I going to send a path ASAP. Have I nice evening))

mamont77 commented 7 years ago

I can't send a patch right now, don't have GIT in the current workspace. However, I fixed it.

Just replace a piece of your code:

    if ($method == 'GET') {
      $fullURL .= $query ? '&' . http_build_query($query) : '';
    }
    else {
      if ($query instanceof CURLFile) {
        curl_setopt($ch, CURLOPT_POST, TRUE); // Enable posting.
        curl_setopt(
          $ch,
          CURLOPT_POSTFIELDS,
          array(
            'attachment' => $query,
          )
        );
      }
      else {
        $Q = array('JSONString' => json_encode($query));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Q));
      }
    }

then call like

            $attachment = __DIR__ . $invoice_attachment;
            $mime_type  = mime_content_type($attachment);
            $file_name  = basename($attachment);
            $parameters = new CURLFile($attachment, $mime_type, $file_name);

            try {
              $result = $zoho->makeApiRequest(
                'invoices/' . $invoice_id . '/attachment',
                'POST',
                $parameters
              );
            } catch (Exception $e) {
              logger('Zoho Exception', $zoho->lastRequest['dataRaw']);

            }