示例PHP代码以编程方式检查Magento 2中的SOAP连接

时间:2016-07-11 14:07:48

标签: php magento soap magento2 magento-2.0.7

我尝试了下面的代码,虽然我不确定这些是否是必需的脚本集,但它没有工作并给出

"SOAP-ERROR: Parsing WSDL: Couldn't load from  : Start tag expected, '<' not found"

$wsdlUrl = 'http://localhost/magento20_0407/soap/default?wsdl_list=1';
$apiUser = 'testUser'; 
$apiKey = 'admin123';
$token = 'xioxnuuebn7tlh8ytu7781t14w7ftwmp';

$opts = array('http' => array('method' => "GET", 'header' => "Accept-language: en\r\nConnection: close\r\n")); 
$context = stream_context_create($opts); 
stream_context_set_option($context, "http", "protocol_version", 1.1); 
fpassthru(fopen($wsdlUrl, 'r', false, $context)); 
$opts = array('http'=>array('header' => 'Authorization: Bearer '.$token));
$serviceArgs = array("id"=>1);

try{
    $context = stream_context_create($opts);
    $soapClient = new SoapClient($wsdlUrl, array('version' => SOAP_1_2, 'context' => $context));
    $soapResponse = $soapClient->customerCustomerAccountServiceV1($serviceArgs);
}catch(Exception $e){
    $e->getMessage();
}
    var_dump($soapResponse);exit;

任何人都可以共享代码以在Magento2.x中建立SOAP连接

在Magento1.x中,以下代码可以很好地连接SOAP

$apiUrl = 'http://localhost/magento_28_03/index.php/api/soap?wsdl';
$apiUser = 'testUser'; 
$apiKey = 'admin123';

ini_set("soap.wsdl_cache_enabled", "0");
try{
    $client = new SoapClient($apiUrl, array('cache_wsdl' => WSDL_CACHE_NONE));
} catch (SoapFault  $e) {
    echo 'Error in Soap Connection : '.$e->getMessage();
}
try {
    $session = $client->login($apiUser, $apiKey);
    if($session) echo 'SOAP Connection Successful.';
    else echo 'SOAP Connection Failed.';

} catch (SoapFault  $e) {
    echo 'Wrong Soap credentials : '.$e->getMessage();
}   

但上述对Magento1.x无效。 谁能说,上面的代码需要对Magent2.x进行哪些更改?

2 个答案:

答案 0 :(得分:2)

对于SOAP API调用,请遵循以下

test.php的     

require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');



$addArgs = array('num1'=>2, 'num2'=>1);// Get Request
$sumArgs = array('nums'=>array(2,1000));// Post request


//$wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=customerAccountManagementV1,customerCustomerRepositoryV1,alanKentCalculatorWebServiceCalculatorV1";//To declar multiple
$wsdlUrl = YOUR_BASE_URL."soap?wsdl&services=alanKentCalculatorWebServiceCalculatorV1";
try{
    $opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
    $context = stream_context_create($opts);

    $soapClient = new \Zend\Soap\Client($wsdlUrl);
    $soapClient->setSoapVersion(SOAP_1_2);
    $soapClient->setStreamContext($context);
}catch(Exception $e){
    echo 'Error1 : '.$e->getMessage();
}

try{
    $soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Add($addArgs);print_r($soapResponse);
    $soapResponse = $soapClient->alanKentCalculatorWebServiceCalculatorV1Sum($sumArgs);print_r($soapResponse);          
}catch(Exception $e){
    echo 'Error2 : '.$e->getMessage();
}

&GT;

http://YOUR_BASE_URL/test.php

答案 1 :(得分:0)

"SOAP-ERROR: Parsing WSDL: Couldn't load from  : Start tag expected, '<' not found"

告诉您所有需要知道的信息,您尝试加载的网址不是正确的WSDL。

以下内容有哪些:http://localhost/magento20_0407/soap/default?wsdl_list=1