1

I am trying to upload an attachment using the following function.

     public function uploadAttachment($attachmentBody, $attachmentBodyLength, $attachmentName) {
    $createFields = array(
        'Body' => utf8_encode($attachmentBody),

    //    'ContentType' => $contentType,
        'Name' => $attachmentName,
        'IsPrivate' => 'false',
    );
    $sObject = new stdclass();
    $sObject->fields = $createFields;
    $sObject->type = 'Attachment';
    $sObject->body = utf8_encode ( $attachmentBody);

    echo "Creating Attachment";
    $upsertResponse = $this->SFConnection->create(array($sObject));
    print_r($upsertResponse);
}

I know that I am not currently specifying a ParentID - this is not my problem

I am getting an error "Caught exception: The element type "Body" must be terminated by the matching end-tag "" when I utf8_encode($attachmentBody). If I remove the utf8_encode I get the infamous "Invalid byte 1 of 1-byte UTF-8 sequence." problem!

Where am I going wrong? Has anyone else successfully uploaded attachments through the PHP API?

1 Answer 1

0

I have managed to figure out a solution to this.

I was using the wrong encoding, after finding this link - http://www.salesforce.com/developer/docs/api/Content/implementation_considerations.htm

And reading the sections around Internationalization and Character Sets and XML Compliance it become apparent that the string (body) that I was uploading was already in UTF-8, and just required Base64 Encoding.

Below is my working code - just in case you are experiencing issues too.

public function uploadAttachment($attachmentBody, $attachmentName) {
        $createFields = array(
            'Body' => base64_encode($attachmentBody),
            //    'ContentType' => $contentType,
            'Name' => $attachmentName,
            'ParentID' => 'AN OBJECT ID',
            'IsPrivate' => 'false'

        );
        $sObject = new stdclass();
        $sObject->fields = $createFields;
        $sObject->type = 'Attachment';

        echo "Creating Attachment";
        $upsertResponse = $this->SFConnection->create(array($sObject));
        print_r($upsertResponse);
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .