1

Been searching for awhile but haven't been able to find any examples.. I'm trying to upload a remote PDF file to an Account record in SF using PHP cURL / Json, is that possible and if so do you have a basic example showing how?

EDIT -- here's the code I'm using:

        define("USERNAME", "[email protected]");
        define("PASSWORD", "MyPasswordandSecurityToken");
        define('CLIENT_ID', 'MYCLIENTID');
        define('CLIENT_SECRET', 'MYCLIENTSECRET');


        $loginurl = "https://test.salesforce.com/services/oauth2/token";

        $params = "grant_type=password"
        . "&client_id=" . CLIENT_ID
        . "&client_secret=" . CLIENT_SECRET
        . "&username=" . USERNAME
        . "&password=" . PASSWORD;

        $curl = curl_init($loginurl);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

        $json_response = curl_exec($curl);

        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ( $status != 200 ) {
            die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }

        curl_close($curl);

        $sfData = json_decode($json_response);

        $access_token = $sfData->access_token;
        $instance_url = $sfData->instance_url;


        $url = $instance_url . "/services/data/v20.0/sobjects/Attachment/";         
        $accId= '0015B000002MzlbQAC';

        $fp = fopen('MyFile.pdf', 'r');
        $content = fread($fp, filesize('MyFile.pdf'));
        $fileData = base64_encode($content);
        $data = array("ParentId" => $accId, "Name" => 'MyFile.pdf', "body" => "$fileData", "IsPrivate" => "false");
        $data_string = json_encode($data);
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Authorization: OAuth $access_token","Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

        $json_response = curl_exec($curl);

        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ( $status != 201 ) {
            die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }

        // echo "HTTP status $status uploading file failed<br/><br/>";
        curl_close($curl);

        $response = json_decode($json_response, true);

        var_dump($response);

Which returns the following 201 Response:

array(3) { ["id"]=> string(18) "00P5B000000NOU4UAO" ["success"]=> bool(true) ["errors"]=> array(0) { } }

However, I'm not seeing the File under the Account or anywhere in Salesforce

1 Answer 1

2

I am hoping that you are using Force.com PHP SDK. You can directly call attachment SOAP API with following code.

        $url = "$instance_url/services/data/v20.0/sobjects/Attachment/";
        $accId='00435DAFDF3';
        //frmResume is <inputfile name
        if(isset($_FILES['frmResume']) && $_FILES['frmResume']!=null)
        {
            $fileName = $_FILES['frmResume']['name'];
            $tmpName  = $_FILES['frmResume']['tmp_name'];
            $fileSize = $_FILES['frmResume']['size'];
            $fileType = $_FILES['frmResume']['type'];

            if($fileSize>0)
            {
                $fp      = fopen($tmpName, 'r');
                $content = fread($fp, filesize($tmpName));
                $fileData = base64_encode($content);
                $data = array("ParentId" => "$accId", "Name" => 'Resume_' . date('Y-m-d H:i:s') . '.pdf', "body" => "$fileData");
                $data_string = json_encode($data);
                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HTTPHEADER,
                array("Authorization: OAuth $access_token","Content-type: application/json"));
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

                $json_response = curl_exec($curl);

                $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

                if ( $status != 201 ) {
                    die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
                }

                // echo "HTTP status $status uploading file failed<br/><br/>";

                curl_close($curl);

                $response = json_decode($json_response, true);

                $attachmentid = $response["id"];
            }

        }
3
  • Thanks for the reply, I am using the PHP SDK, trying your example above but getting the following error: Notice: Undefined variable: access_token
    – AJK
    Commented Jul 20, 2016 at 21:22
  • I updated my code again and now getting a 201 response, however I don't see the PDF file uploaded anywhere in SF, any idea what I did wrong? Thanks!
    – AJK
    Commented Jul 21, 2016 at 21:53
  • Ahh, finally got it working.. stupid mistake, I was expecting it to show up on the Account under the "Files" related list, but it was under "Notes and Attachments" (doh!).. so I added that to my page layout and can now see the files. Thanks!
    – AJK
    Commented Jul 22, 2016 at 15:02

You must log in to answer this question.

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