1

我正在尝试使用 Salesforce 元数据 REST API 将元数据记录从 PHP 部署到 salesforce 组织。 https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_rest_deploy.htm

根据文档,我们应该在子部分中发送数据

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--

下面是我的PHP代码

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);
    }

它总是向我抛出错误发生意外错误。如果您联系支持人员,请包含此 ErrorId:1629748103-90297 (-1851675661)

任何人都可以帮助我在包括 zip 文件在内的子部分中发送请求正文。

4

0 回答 0