如何使用 PHP-curl 发送“多部分/相关”内容类型的请求?

Posted

技术标签:

【中文标题】如何使用 PHP-curl 发送“多部分/相关”内容类型的请求?【英文标题】:How to send request for "multipart/related" Content-Type using PHP-curl? 【发布时间】:2018-11-20 14:13:17 【问题描述】:

以下是肥皂信封和标题信息:

    POST /tf6/services/xdsrepositoryb HTTP/1.1
    Content-Type: multipart/related; 
    boundary=MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449; 
    type="application/xop+xml"; start="
    <0.urn:uuid:566EAD10FEBB55C5A61257193478450@apache.org>"; start-
    info="application/soap+xml"; 
    action="urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b"

    --MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449
    Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"
    Content-Transfer-Encoding: binary
    Content-ID: <0.urn:uuid:566EAD10FEBB55C5A61257193478450@apache.org>

    <?xml version='1.0' encoding='UTF-8'?>
    <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
        xmlns:wsa="http://www.w3.org/2005/08/addressing">
        <soapenv:Header>
            <wsa:To>http://localhost:5000/tf6/services/xdsrepositoryb</wsa:To>
            <wsa:MessageID soapenv:mustUnderstand="true">urn:uuid:566EAD10FEBB55C5A61257193478400</wsa:MessageID>
            <wsa:Action>urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b</wsa:Action>
        </soapenv:Header>
        <soapenv:Body>
            <xdsb:ProvideAndRegisterDocumentSetRequest xmlns:xdsb="urn:ihe:iti:xds-b:2007">
     <lcm:SubmitObjectsRequest xmlns:lcm="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0">
                    <rim:RegistryObjectList xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
                        <rim:ExtrinsicObject id="Document01" mimeType="text/plain"
                            objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
     </lcm:SubmitObjectsRequest>
      <xdsb:Document id="Document01">
                    <xop:Include href="cid:1.urn:uuid:566EAD10FEBB55C5A61257193478499@apache.org"
                        xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
                </xdsb:Document>
            </xdsb:ProvideAndRegisterDocumentSetRequest>
        </soapenv:Body>
    </soapenv:Envelope>

    --MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449
    Content-Type: text/plain
    Content-Transfer-Encoding: binary
    Content-ID: <1.urn:uuid:566EAD10FEBB55C5A61257193478499@apache.org>
    This is my document.
    It is great!
    --MIMEBoundaryurn_uuid_566EAD10FEBB55C5A61257193478449--

如何使用 CURL 编写 php 脚本来发送请求?

每当我尝试调用服务时都会收到 HTTP-400 错误,请告知。

谢谢。

【问题讨论】:

添加你试过的php代码 我已将以下代码作为答案发布,请检查。 【参考方案1】:

这是我正在尝试的代码:

 $body= $body = '<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
    <a:Action s:mustUnderstand="1">urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b</a:Action>
    <a:From>
        <a:Address>urn:oid:2.16.840.1.113883.3.1259.10.1003</a:Address>
    </a:From>
    <a:To>https://CDACert.hawaiihie.org:20000/repository</a:To>
</s:Header>
<s:Body>
    <ProvideAndRegisterDocumentSetRequest xmlns="urn:ihe:iti:xds-b:2007" xmlns:xop="http://www.w3.org/2004/08/xop/include">
        <lcm3:SubmitObjectsRequest xmlns:lcm3="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
            <rim:RegistryObjectList>
                <rim:ExtrinsicObject id="Document1" mimeType="text/xml" objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
                </rim:Association>
            </rim:RegistryObjectList>
        </lcm3:SubmitObjectsRequest>
        <Document id=\"Document1\">
                <xop:Include href="cid:test_ID_123"
                xmlns:xop="http://www.w3.org/2004/08/xop/include"/></Document>
    </ProvideAndRegisterDocumentSetRequest>
</s:Body>

';

 $text = 'dGhpcyBpcyBTaGl2J3MgZG9j';// base64 of content
 $boundary  = 'MIMEBoundaryurn_uuid_'.uniqid();
 $postData = "--$boundary\n"
     ."Content-Type: application/xop+xml; charset=UTF-8; type=\"application/soap+xml\n"
     ."Content-Transfer-Encoding: binary\n"
     ."Content-length:".strlen($body)."\n"
     ."Content-ID:test_start\n"
     .$body."\n\n"
     ."--$boundary\n"
     ."Content-Type: text/plain;\n"
     ."Content-Transfer-Encoding: binary\n"
     ."Content-length:".strlen($text)."\n"
     ."Content-ID:test_ID_123\n"
     .$text."\n"
     ."--$boundary--";
 $header1 = array (
          "Content-Type: multipart/related; type=\"application/xop+xml\"; start=\"test_start\"; start-info=\"application/soap+xml\"; action=\"urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-b\"",
          "Content-length:".strlen($postData),
          "Transfer-Encoding:chunked",
          "User-Agent:" .$_SERVER ['HTTP_USER_AGENT'],
          "Host:" .$_SERVER['HTTP_HOST']
     );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLINFO_, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array($postData)); // the SOAP request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch); // 400 HTTP error from curl_info

【讨论】:

以上是关于如何使用 PHP-curl 发送“多部分/相关”内容类型的请求?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 中提供多部分/相关内容?

如何在 Ubuntu 16.04 中安装 php-curl

作曲家没有看到php-curl

阿里云服务器怎么开启curl

PHP-cURL接收xml

PHP-curl