如何在PHP中发出SOAP请求

如何在PHP中发出SOAP请求,php,soap,soap-client,Php,Soap,Soap Client,我正在尝试用PHP发出SOAP请求。我有我的服务URL,当我在SOAP UI中检查它时,我可以看到以下内容 <application xmlns="http://somenamespace.com"> <doc xml:lang="en" title="https://someurl.com"/> <resources base="https://someurl.com"> <resource path="sdk/user/ses

我正在尝试用PHP发出SOAP请求。我有我的服务URL,当我在SOAP UI中检查它时,我可以看到以下内容

<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);
}
但是,我总是收到登录失败的错误响应。我必须调用登录方法还是什么? 我只是想知道我是否做得对


谢谢

根据XML,您应该尝试将curl\u post\u数据变量作为URL编码字符串发送。例如
urlencode('ApiKey=somekey&ApiSecret=somesecret')
,然后尝试将请求的内容类型设置为“
application/x-www-form-urlencoded

您没有设置
content-type
标题,告诉您发布的内容格式:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                      'Content-Type: application/x-www-form-urlencoded'));
否则,从php5开始,建议使用
http\u build\u query

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

curl_setopt($curl, CURLOPT_POSTFIELDS,
            http_build_query($curl_post_data));
希望它能帮助你,
Thierry

您尝试访问的服务的文档可能会解释他们的RESTful服务以及如何使用它。你能分享你想使用的API吗?也许有人编写了包装器,可以用来简化您的工作。不幸的是,这是一个私有API,除非您登录,否则文档不可用。文档只显示非常基本的注释。我已经搜索过了,但没有任何包装。您的请求中是否缺少所需的
方法
参数?请尝试将curl\u post\u数据变量作为URL编码字符串发送。比如urlencode('ApiKey=somekey&ApiSecret=somesecret'),然后尝试将请求的内容类型设置为“application/x-www-form-urlencoded”
$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);