使用php Xero api在Xero中添加附件

使用php Xero api在Xero中添加附件,php,xero-api,Php,Xero Api,有人能帮我使用官方(非calcanai)PHP SDK将pdf添加到Xero发票中吗。我已连接到oAuth2,并且之前已经创建了发票草稿 然后,我将发票更新为“已授权”,并尝试向发票添加附件。在这一点上,我没有得到任何运气,附件被发送,但在响应中返回为空。这些文档一点用处都没有,也没有给出任何添加附件甚至最小字段的示例。这就是我所拥有的: ... $attachments = $apiResponse->getInvoices()[0]->getAttachments

有人能帮我使用官方(非calcanai)PHP SDK将pdf添加到Xero发票中吗。我已连接到oAuth2,并且之前已经创建了发票草稿

然后,我将发票更新为“已授权”,并尝试向发票添加附件。在这一点上,我没有得到任何运气,附件被发送,但在响应中返回为空。这些文档一点用处都没有,也没有给出任何添加附件甚至最小字段的示例。这就是我所拥有的:

...
        $attachments = $apiResponse->getInvoices()[0]->getAttachments();
        $attachment = new XeroAPI\XeroPHP\Models\Accounting\Attachment;
        $attachment->setFileName( $filename )
            ->setIncludeOnline( true )
            ->setMimeType( 'application/pdf' );
        $attachments[] = $attachment;
        $apiResponse->getInvoices()[0]->setAttachments( $attachments );
        $apiResult = $accountingApi->updateOrCreateInvoices( $xeroTenantId, $apiAuth );
            if ( !$apiResult->getInvoices()[0]->getHasAttachments() ) {
                $errors[] = 'Pdf file was not added to Xero.';
            }
            $errorList = array();
            $errList = $apiResult->getInvoices()[0]->getValidationErrors()[0];
            if ( !is_null( $errList ) ) {
                $errorList = $errList;
            }
            foreach ( $errorList as $err ) {
                $errors[] = $err->getMessage();
            }
            if ( count( $errors ) == 0 ) {
                $result['message'] = 'New Invoice Authorised: ' . $xeroInvoice->getReference();
                $result['apiResult'] = $apiResult;
                $result['success'] = true;
            } else {
                $result['message'] = print_r( $errors );
                $result['success'] = false;
            }
...
有什么想法吗

谢谢

*后面的代码*

public function attachPDF( $xeroTenantId, $accountingApi, $invoice ) {
        $filename = 'AUTH#' . sprintf( '%08d', $invoice->id ) . '.pdf';
        $guid     = $invoice->invoice_id;
        $content  = $invoice->getPDF( \Mpdf\Output\Destination::FILE, true, $filename );
        $handle = fopen( $filename, "r" );
        $contents = fread( $handle, filesize( $filename ) );
        fclose( $handle );
        unlink( $filename );
        return $accountingApi->createInvoiceAttachmentByFileName( $xeroTenantId, $guid, $filename, $contents, true );
    }

添加附件需要两个API调用

//创建或获取发票ID,然后创建附件

    $invoices = $apiInstance->getInvoices($xeroTenantId);                       
    $guid = $invoices->getInvoices()[0]->getInvoiceId();

    // file in the same dir.        
    $filename = "./helo-heros.jpg";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);

    $result = $apiInstance->createInvoiceAttachmentByFileName($xeroTenantId,$guid,"helo-heros.jpg",$contents);


添加附件需要两个API调用

//创建或获取发票ID,然后创建附件

    $invoices = $apiInstance->getInvoices($xeroTenantId);                       
    $guid = $invoices->getInvoices()[0]->getInvoiceId();

    // file in the same dir.        
    $filename = "./helo-heros.jpg";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);

    $result = $apiInstance->createInvoiceAttachmentByFileName($xeroTenantId,$guid,"helo-heros.jpg",$contents);


任何其他人都有同样的问题,这是因为文件名中有一个#(在我的示例中为AUTH#00000123.pdf)基于Sidney答案的更新代码也是前进的方向

任何其他人都有同样的问题,这是因为文件名中有一个#(在我的示例中为AUTH#00000123.pdf)基于sidneys答案的更新代码也是前进的方向

我曾经尝试过,但使用了错误的类($apiInstance)。我有一点进一步,但现在我得到一个异常-该文件无法上传,因为它不是一个受支持的文件类型。文件是pdf格式的吗?我已经用新的更改更新了我的原始帖子。我已经为这个问题创建了一个新的票证。我尝试了这个,但是使用了错误的类($apiInstance)。我有一点进一步,但现在我得到一个异常-该文件无法上传,因为它不是一个受支持的文件类型。文件是pdf格式的吗?我已经用新的更改更新了我原来的帖子。我已经为这个问题创建了一个新的票证。