简体   繁体   English

无法使用php CURL进行API调用

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

This is the request format for the SOAP request 这是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>

I tried the following codes below, 我在下面尝试了以下代码,

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

This page loaded for sometime and I got '0' on the page. 此页面加载了一段时间,并且页面上显示“ 0”。

The actual xml response should be 实际的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> 

Im new to SOAP,can't find a better way..I got the above code online,I dont know this is the way a SOAP request should be done. 我是SOAP的新手,找不到更好的方法。.我在网上获得了上述代码,我不知道这是应该完成SOAP请求的方法。

Please suggest some ways to do it.Thanks guys 请建议一些方法。谢谢大家

What you would want to do is to check if there is a curl error by adding this after executing the curl 您想要做的是通过在执行卷曲之后添加此错误来检查是否存在卷曲错误

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

From here on, determine whether if it is a curl error. 从这里开始,确定是否是卷曲错误。 If it is, post the error. 如果是这样,请发布错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM