2012-01-26 4 views
3

Я пытаюсь выполнить SOAP-функцию с помощью Curl (потому что я получаю сообщение об ошибке с помощью SoapClient().Выполнение SOAP с помощью Curl

Это мой код (который наполовину работает)

$credentials = "username:pass"; 
$url = "https://url/folder/sample.wsdl"; 
$page = "/folder"; 
$headers = array( 
    "POST ".$page." HTTP/1.0", 
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Cache-Control: no-cache", 
    "Pragma: no-cache", 
    "SOAPAction: \"customerSearch\"", 
    "Authorization: Basic " . base64_encode($credentials) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']); 
$data = curl_exec($ch); 

Проблема заключается в том, что SOAP-действие не выполняется. И мне также нужно передать аргументы в действие. Возможно ли это?

ответ

11

Необходимо указать опции cURL для POST и установить тело запрос - если вы не отправляете тело, в POST-запросе нет смысла (и что более важно, i t не является SOAP). Построение полного заголовка HTTP-запроса просто не сократит его.

$credentials = "username:pass"; 
$url = "https://url/folder/sample.wsdl"; 
$body = ''; /// Your SOAP XML needs to be in this variable 

$headers = array( 
    'Content-Type: text/xml; charset="utf-8"', 
    'Content-Length: '.strlen($body), 
    'Accept: text/xml', 
    'Cache-Control: no-cache', 
    'Pragma: no-cache', 
    'SOAPAction: "customerSearch"' 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']); 

// Stuff I have added 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_USERPWD, $credentials); 

$data = curl_exec($ch); 
Смежные вопросы