PHP如何使用两个级别的Curl设置自定义标头

时间:2016-09-01 13:54:35

标签: php curl soap

我正在尝试创建一个将POST请求发送到Web服务的php。

使用SoapUI尝试过服务后,我知道XML POST REQUEST应该是这样的:

<soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:ns="http://etim.nl/xlmschemas/messageservice/2.40">
<soapenv:Header>
<ns:CustomInfo>
<ns:IsTestMessage>true</ns:IsTestMessage>
<!--Optional:-->
<ns:IsContentCompressed>false</ns:IsContentCompressed>
</ns:CustomInfo>
<ns:AuthenticationInfo>
<ns:ApplicationId>SomeId</ns:ApplicationId>
<ns:VersionId>0.9</ns:VersionId>
<ns:RelationId></ns:RelationId>
<ns:UserId>SomeUserId</ns:UserId>
<ns:Password>SomePassword</ns:Password>
</ns:AuthenticationInfo>
</soapenv:Header>
<soapenv:Body>
<!--etc...(actual xml)-->
</soapenv:Body>
</soapenv:Envelope>

如何使用Curl在php中添加自定义标题?

希望我足够清楚..(顺便说一句,我不能使用wsdl)

编辑:没有可用的wsdl 编辑2:

My Current Curl:

<?php 

        $soapUrl = "https://services.dev-cloud.nl/services/KovraPostMessageRequestInboundProxy"; // asmx URL of WSDL
        $soapUser = "SomeUsername";  //  username
        $soapPassword = "SomePassword"; // password

        // xml post structure

$xml_post_string = '<soapenv:Body>
                    <MaintenanceStatus xmlns="http://www.gs1.nl/onderhoudstatus/insbou/001" schemaLocation="http://www.gs1.nl/onderhoudstatus/insbou/001 Onderhoudstatus_insbou001.xsd">
                    <MessageNumber>6047</MessageNumber>
                    <MessageDate>2016-08-15</MessageDate>
                    <MessageTime>08:48:39</MessageTime>
                    <InstructionData>
                    <InstructionNumber>137493.0</InstructionNumber>
                    <Status>ANN</Status>
                    <StatusDescription>Opdracht geannuleerd</StatusDescription>
                    <DateReady>2016-08-15</DateReady>
                    <FreeText>Vervallen. Doorgebeld 15-8 door Edith</FreeText>
                    <AppointmentDateTimeInformation>
                    <RequiredDeliveryDate>2016-08-17</RequiredDeliveryDate>
                    <RequiredDeliveryTime>11:00:00</RequiredDeliveryTime>
                    <DeliveryTimeFrame>
                    <DeliveryDateEarliest>2016-08-17</DeliveryDateEarliest>
                    <DeliveryTimeEarliest>11:00:00</DeliveryTimeEarliest>
                    <DeliveryDateLatest>2016-08-26</DeliveryDateLatest>
                    <DeliveryTimeLatest>00:00:00</DeliveryTimeLatest>
                    </DeliveryTimeFrame>
                    </AppointmentDateTimeInformation>
                    </InstructionData>
                    </MaintenanceStatus>
                    </soap:Body>';

            $headers = array(
                        "Content-type: text/xml;charset=\"utf-8\"",
                        "Accept-Encoding: gzip,deflate",
                        "Cache-Control: no-cache",
                        "Pragma: no-cache",
                        "SOAPAction: https://services.dev-cloud.nl/services/KovraPostMessageRequestInboundProxy",
                        "Content-length: ".strlen($xml_post_string),
                        "Host: services.dev-cloud.nl",
                        "Connection: Keep-Alive",
                    );

            $url = $soapUrl;

            // PHP cURL  for https connection with auth
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // XML REQUEST
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);

            //should i first create array?
            $CustomInfo = array();
            $CustomInfo[] = 'ns:IsTestMessage: true';
            $CustomInfo[] = 'ns:IsContentCompressed: false';

            //should i first create array?
            $AuthenticationInfo = array();
            $AuthenticationInfo[] = 'ns:ApplicationId: Reparatieverzoeken';
            $AuthenticationInfo[] = 'ns:VersionId: 0.9';
            $AuthenticationInfo[] = 'ns:RelationId:';
            $AuthenticationInfo[] = 'ns:UserId: '.$soapUser;
            $AuthenticationInfo[] = 'ns:Password: '.$soapPassword;


            $headers = array();
            $headers[] = 'ns:CustomInfo';//this is where i dont know how to go about!!!


            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


            // converting
            $response = curl_exec($ch); 
            curl_close($ch);

            // converting
            $response1 = str_replace("<soapenv:Body>","",$response);
            $response2 = str_replace("</soapenv:Body>","",$response1);

            // convertingc to XML
            $parser = simplexml_load_string($response2); 
            print_r($parser);
?>

0 个答案:

没有答案