How to call Salesforce metadata rest api from curl php

832 views Asked by At

I am trying to deploy metadata records from PHP to salesforce org using salesforce metadata REST API. https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_rest_deploy.htm

As per documentation, we are supposed to send the data in subparts

POST /services/data/v48.0/metadata/deployRequest
Authorization: Bearer 00D....
Content-Type: multipart/form-data; boundary=--------------------------BOUNDARY
----------------------------BOUNDARY
Content-Disposition: form-data; name="json"
Content-Type: application/json
{ 
    "deployOptions" :
        {
        "allowMissingFiles" : false,
        "autoUpdatePackage" : false,
        "checkOnly" : false,
        "ignoreWarnings" : false,
        "performRetrieve" : false,
        "purgeOnDelete" : false,
        "rollbackOnError" : false,
        "runTests" : null,
        "singlePackage" : true,
        "testLevel" : "RunAllTestsInOrg"
        }
    }
----------------------------BOUNDARY
Content-Disposition: form-data; name="file"; filename="deploy.zip"
Content-Type: application/zip

//Contents of deploy.zip
----------------------------BOUNDARY--

Below is my PHP code

public function deployCustomMetadata($serviceUrl, $sessionid){
        
        $posturl = $serviceUrl.'services/data/v48.0/metadata/deployRequest';
        $filedata = file_get_contents(PACKAGE_PATH);

        list($request_headers, $form) = $this->create_upload_form_for_datachunk($filedata, $sessionid);
        $fp = fopen('./packages//errorlog.txt', 'w');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $posturl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, true);         
        curl_setopt($ch, CURLOPT_POSTFIELDS, implode("\r\n", $form));
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_STDERR, $fp);
        $data = curl_exec($ch); 
        print_r($data);
    }

    function create_upload_form_for_datachunk($data, $sessionid) {
        $deployOptions = json_encode(array("deployOptions" => 
                array(
                        "allowMissingFiles" => false,
                        "autoUpdatePackage" => false,
                        "checkOnly" => false,
                        "ignoreWarnings" => false,
                        "performRetrieve" => false,
                        "purgeOnDelete" => false,
                        "rollbackOnError" => false,
                        "runTests" => null,
                        "singlePackage" => true,
                        "testLevel" => "RunAllTestsInOrg"
                    )
        ));
       
        $headers = array();
        $form[] = implode("\r\n", array(
            "Content-Disposition: form-data; name=\"filedata\"; filename=\"blob\"",
            "Content-Type: application/octet-stream",
            "",
            $data,
            "blob"
        ));
     
        // generate safe boundary
        do {
            $boundary = "---------------------" . md5(mt_rand() . microtime());
        } while (preg_grep("/{$boundary}/", $form));
     
        // add boundary for each parameters
        array_walk($form, function (&$part) use ($boundary) {
            $part = "--{$boundary}\r\n{$part}";
        });
     
        // add final boundary
        $form[] = "--{$boundary}--";
        $form[] = "";
     
        // set options
        $headers[] = "Authorization: Bearer  ".$sessionid;
        $headers[] = "Content-Type: multipart/form-data; boundary={$boundary}";
       
        $headers[] = "Content-Disposition: form-data; name=\"json\" ";
        $headers[] = "Content-Type: application/json ".$deployOptions.$boundary;
        $headers[] = "Content-Disposition: form-data; name=\"file\"  ; filename=\"deploy.zip\"  ";
        $headers[] = "Content-Type: application/zip".$boundary;
        
        return array($headers, $form);
    }

It always throws me error An unexpected error occurred. Please include this ErrorId if you contact support: 1629748103-90297 (-1851675661)

Can anyone help me in sending request body in subparts including the the zip file.

0

There are 0 answers