簡體   English   中英

用於在Magento 2中以編程方式檢查SOAP連接的示例php代碼

[英]Sample php code to programmatically check SOAP connection in Magento 2

盡管不確定這些腳本是否是必需的腳本集,但是我嘗試了以下代碼,但是它無法正常工作並給出

"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正常工作,上述代碼需要進行哪些更改?

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

?>

HTTP://YOUR_BASE_URL/test.php

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

告訴您所有您需要了解的信息,您嘗試加載的URL不是正確的WSDL。

內容是什么: http:// localhost / magento20_0407 / soap / default?wsdl_list = 1

暫無
暫無

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

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