10

I am trying to issue a SOAP request in PHP. I have my service URL, and when I check it in SOAP UI, I can see the following

<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>

So I am trying to use this to log on. At the moment, I am trying something like the following

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);
}

However, I always receive a fault response that the login has failed. Do I have to call the logon method or something? Really just looking for some advice as to whether I am doing things correctly.

Thanks

4
  • 3
    Docs for the service you are trying to hit might have explanation of their RESTful services and how to use it. Can you share which API you are trying to hit? Perhaps someone has written wrappers that you could use to simplify your work. Commented Dec 6, 2015 at 19:29
  • Unfortunately its a private API, docs are not available unless you log in. Docs only show very basic notes. I have searched and there are no wrappers anywhere. Commented Dec 6, 2015 at 21:00
  • Is it possible that you're missing the required method parameter in your request? Commented Dec 7, 2015 at 8:36
  • Try to send your curl_post_data variable as a URL Encoded string. like urlencode('ApiKey=somekey&ApiSecret=somesecret') and secondly try to set the content-type of your request to 'application/x-www-form-urlencoded'' Commented Dec 8, 2015 at 10:19

3 Answers 3

7
+25

You don't set the Content-Type header telling the format of the content you posted:

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

Otherwise, from php5 upwards, the usage of http_build_query is recommended:

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

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

Hope it helps you, Thierry

Sign up to request clarification or add additional context in comments.

Comments

6

According the XML you should try to send your curl_post_data variable as a URL Encoded string. like urlencode('ApiKey=somekey&ApiSecret=somesecret') and secondly try to set the content-type of your request to 'application/x-www-form-urlencoded'

Comments

6
$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.