簡體   English   中英

無法使用php CURL進行API調用

[英]Not able to make API call using php CURL

這是SOAP請求的請求格式

<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 

xmlns:sas="http://api.testdemo.in/"> 

<SOAP-ENV:Body> 

 <sas:Login> 

 <Username>user</Username> 

 <Password>password</Password> 

 </sas:Login> 

</SOAP-ENV:Body> 

</SOAP-ENV:Envelope>

我在下面嘗試了以下代碼,

$xml = '<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sas="http://api.testdemo.in/">
<SOAP-ENV:Body>
 <sas:Login>
 <Username>user</Username>
 <Password>password</Password>
 </sas:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

$url = "http://api.testdemo.in/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
if($xml != null) {
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
    array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($response);
print_r($code);

此頁面加載了一段時間,並且頁面上顯示“ 0”。

實際的xml響應應該是

<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 

xmlns:sas="http://api.testdemo.in/"> 

<SOAP-ENV:Body> 

 <sas:LoginResponse> 

 <SessionID>FC82329EF6</SessionID> 

 <ResponseCode>0</ResponseCode> 

 <ResponseMessage>Successsful</ResponseMessage>

 </sas:LoginResponse> 

</SOAP-ENV:Body> 

</SOAP-ENV:Envelope> 

我是SOAP的新手,找不到更好的方法。.我在網上獲得了上述代碼,我不知道這是應該完成SOAP請求的方法。

請建議一些方法。謝謝大家

您想要做的是通過在執行卷曲之后添加此錯誤來檢查是否存在卷曲錯誤

try
{
    $response = curl_exec($ch);

    if(curl_errno($ch) == "0")    //check if there is an curl error occurred such as unable to connect to host etc
    {
         //do your parsing here

    }
    else
    {
        throw new exception('Curl Error No'.curl_errno($ch).' Error Description:'.curl_error($ch));
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}

從這里開始,確定是否是卷曲錯誤。 如果是這樣,請發布錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM