如何在php中实现soap请求?

时间:2018-10-03 06:32:44

标签: php soap

我在这个网站上搜索了有关在PHP中集成SOAP请求的有用答复,但是没有找到解决我问题的方法。

我是SOAP的新手,无法弄清楚为什么我没有得到任何答复:

 <?php
$xml = '<?xml version="1.0" encoding="utf-8"?>'.
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
    ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'.
    ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'.
        '<soap:Body>'.
            '<food xmlns="http://www.localhost:81/dbWIP/xml1.xml">'.
                '<price>$5.95</price>'.
            '</food>'.
        '</soap:Body>'.
    '</soap:Envelope>';

$url = "http://www.localhost:81/dbWIP/xml1.xml";

$ch = curl_init();
echo $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction:http://www.localhost:81/dbWIP/xml1.xml");
if($xml != null) {
    echo $xml;
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
    array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<br/> CODE:"$code;
curl_close($ch);
?>

2 个答案:

答案 0 :(得分:0)

我认为您缺少curl_init的URL,它将初始化cURL会话,但您的URL没有。

    $curl = curl_init("http://www.localhost:81/dbWIP/xml1.xml");
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($curl);

并尝试将标题设置为

   $headers = array(
        'Content-type: text/xml;charset="utf-8"',
        'Accept: text/xml',
        'Cache-Control: no-cache',
        'Pragma: no-cache',
        'Content-length: ' . strlen($xml),
    );

答案 1 :(得分:0)

尝试删除引号:

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);