Подтвердить что ты не робот

Как выпустить запрос SOAP в PHP

Я пытаюсь выпустить SOAP-запрос в PHP. У меня есть URL-адрес службы, и когда я проверяю его в пользовательском интерфейсе SOAP, я вижу следующее

<application xmlns="http://somenamespace.com">
   <doc xml:lang="en" title="https://someurl.com"/>
   <resources base="https://someurl.com">
      <resource path="sdk/user/session/logon/" id="Logon">
         <doc xml:lang="en" title="Logon"/>
         <param name="ApiKey" type="xs:string" required="false" default="" style="query" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
         <param name="ApiSecret" type="xs:string" required="false" default="" style="query" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
         <method name="POST" id="Logon">
            <doc xml:lang="en" title="Logon"/>
            <request>
               <param name="method" type="xs:string" required="true" default="" style="query" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
               <representation mediaType="application/json"/>
               <representation mediaType="application/xml"/>
               <representation mediaType="text/xml"/>
               <representation mediaType="application/x-www-form-urlencoded"/>
            </request>
            <response status="404 500">
               <representation mediaType="text/html; charset=utf-8" element="html"/>
            </response>
            <response status="">
               <representation mediaType="application/json"/>
               <representation mediaType="application/xml"/>
               <representation mediaType="text/xml"/>
               <representation mediaType="application/x-www-form-urlencoded"/>
            </response>
            <response status="500">
               <representation mediaType="application/vnd.marg.bcsocial.result-v1.9+json; charset=utf-8" element="log:Fault" xmlns:log="https://someurl.com/sdk/user/session/logon"/>
               <representation mediaType="application/vnd.marg.bcsocial.result-v1.9+xml; charset=utf-8" element="web:Result_1" xmlns:web="https://someurl.com/Sdk/WebService"/>
            </response>
            <response status="200">
               <representation mediaType="application/vnd.marg.bcsocial.api.index.options.list-v2.6+xml; charset=utf-8" element="web:ListOfApiIndexOptions_4" xmlns:web="https://someurl.com/Sdk/WebService"/>
               <representation mediaType="" element="data"/>
            </response>
         </method>
      </resource>
   </resources>
</application>

Поэтому я пытаюсь использовать это для входа в систему. На данный момент я пытаюсь сделать что-то вроде следующего

public function updateApi(){
    $service_url = 'https://someurl.com/sdk/user/session/logon';
    $curl = curl_init($service_url);
    $curl_post_data = array(
        "ApiKey" => 'somekey',
        "ApiSecret" => 'somesecret',
    );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    $curl_response = curl_exec($curl);
    curl_close($curl);

    var_dump($curl_response);
}

Тем не менее, я всегда получаю ответ об ошибке, который не удалось выполнить при входе в систему. Должен ли я вызвать метод входа в систему или что-то еще? На самом деле просто ищу некоторые советы относительно того, правильно ли я делаю.

Спасибо

4b9b3361

Ответ 1

Вы не устанавливаете заголовок Content-Type, указывающий формат размещенного вами контента:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                      'Content-Type: application/x-www-form-urlencoded'));

В противном случае, начиная с php5, рекомендуется использовать http_build_query:

$curl_post_data = array(
    "ApiKey" => 'somekey',
    "ApiSecret" => 'somesecret',
);

curl_setopt($curl, CURLOPT_POSTFIELDS,
            http_build_query($curl_post_data));

Надеюсь, это поможет вам, Thierry

Ответ 2

В соответствии с XML вы должны попытаться отправить переменную curl_post_data в виде строки с кодировкой URL. например, urlencode('ApiKey=somekey&ApiSecret=somesecret') и, во-вторых, попытайтесь установить тип содержимого вашего запроса на "application/x-www-form-urlencoded"

Ответ 3

$service_url = 'https://someurl.com/sdk/user/session/logon';
$curl = curl_init($service_url);
$headers = ["Content-Type: application/json"];  // or other supported media type
$curl_post_data = array(
    "ApiKey" => 'somekey',
    "ApiSecret" => 'somesecret',
);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($rest, CURLOPT_HTTPHEADER,$headers);  
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
$curl_response = curl_exec($curl);
curl_close($curl);