2015-12-04 24 views
10

Tôi đang cố đưa ra yêu cầu SOAP bằng PHP. Tôi có URL dịch vụ của mình và khi tôi kiểm tra nó trong giao diện người dùng SOAP, tôi có thể thấyCách đưa ra yêu cầu SOAP trong PHP

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

Vì vậy, tôi đang cố sử dụng tính năng này để đăng nhập. Hiện tại, tôi đang thử một số nội dung như sau:

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

Tuy nhiên, tôi luôn nhận được phản hồi lỗi khi đăng nhập thất bại. Tôi có phải gọi phương thức đăng nhập hay gì đó không? Thực sự chỉ tìm kiếm một số lời khuyên về việc liệu tôi có làm đúng hay không.

Cảm ơn

+3

Tài liệu cho dịch vụ bạn đang cố gắng truy cập có thể có giải thích về dịch vụ RESTful của họ và cách sử dụng dịch vụ. Bạn có thể chia sẻ API nào bạn đang cố gắng truy cập không? Có lẽ ai đó đã viết trình bao bọc mà bạn có thể sử dụng để đơn giản hóa công việc của mình. – zedfoxus

+0

Thật không may là một API riêng tư, tài liệu không có sẵn trừ khi bạn đăng nhập. Tài liệu chỉ hiển thị các ghi chú rất cơ bản. Tôi đã tìm kiếm và không có trình bao bọc ở bất kỳ đâu. –

+0

Có thể bạn đang thiếu tham số 'phương thức' bắt buộc trong yêu cầu của mình không? – Val

Trả lời

6

Theo XML bạn nên cố gắng gửi biến curl_post_data của bạn dưới dạng chuỗi được mã hóa URL. như urlencode('ApiKey=somekey&ApiSecret=somesecret') và thứ hai cố gắng thiết lập các content-type yêu cầu của bạn để 'application/x-www-form-urlencoded'

7

Bạn không thiết lập các tiêu đề Content-Type kể định dạng của nội dung mà bạn đăng tải:

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

Nếu không, từ php5 trở lên, việc sử dụng http_build_query được đề nghị:

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

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

Hy vọng nó giúp bạn, Thierry

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); 
Các vấn đề liên quan