【问题标题】:Sample PHP code to programmatically check SOAP connection in Magento 2示例 PHP 代码以编程方式检查 Magento 2 中的 SOAP 连接
【发布时间】:2016-11-13 12:36:08
【问题描述】:

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

SOAP-ERROR: Parsing WSDL: Couldn't load from : Start tag expected, '

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

但以上不适用于 Magento 1。谁能说,上面的代码需要做哪些更改才能在 Magento 2 上正常工作?

【问题讨论】:

    标签: php magento soap magento2


    【解决方案1】:

    对于 SOAP API 调用,请按照以下方式进行

    test.php

    <?php
    $token = 'YOUR_ACCESS_TOKEN';
    
    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

    【讨论】:

      【解决方案2】:

      SOAP-ERROR: Parsing WSDL: Couldn't load from : Start tag expected, '

      告诉你所有你需要知道的;您尝试加载的 URL 不是正确的 WSDL。

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

      【讨论】:

      • 它是一个 xml 响应,在 Magento1.x 的情况下我们通过运行 apiUrl 得到相同的响应
      • 根据你得到的错误,它不是一个以
      • 我检查了那个 xml 的视图源。一切都被正确定义。而且这种格式来自 Magento 对吧?
      • 文件开头是否有空格或 __HERE__
      • 我复制了视图源代码并粘贴到一个文本文件中。彻底检查了。根本没有空间。这只是一个新行。但没有空间。
      猜你喜欢
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2010-11-07
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多