m2 调用第三方api(soap)脚本范例
m2 调用第三方api(soap)脚本范例
比如获取erp的最新产品库存。
脚本放在项目根目录/cli/testSoap.php
<?php if (php_sapi_name() != "cli")die; include('app/bootstrap.php'); use MagentoFrameworkAppBootstrap; use MagentoFrameworkAppObjectManager; use MagentoFrameworkAppFilesystemDirectoryList; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $url = MagentoFrameworkAppObjectManager::getInstance(); $storeManager = $url->get('MagentoStoreModelStoreManagerInterface'); $mediaurl= $storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA); $state = $objectManager->get('MagentoFrameworkAppState'); $state->setAreaCode('adminhtml'); $mediaPath = $objectManager->get('MagentoFrameworkAppFilesystemDirectoryList')->getPath("media"); $resource = $objectManager->get('MagentoFrameworkAppResourceConnection'); $connection = $resource->getConnection(); $write = $resource->getConnection('core_write';); // Credentials $wsdl = "https://webservice.xxx/UserServices.svc?wsdl"; $username = "用户名"; $password = "密码"; $database = "数据库名"; $pastDays = 2; // Login and get SessioID $sessionId = ""; //valid 15min $logInParam = new stdClass(); $logInParam->oLogInInfo = new stdClass(); $logInParam->oLogInInfo->CompanyDatabase = $database; $logInParam->oLogInInfo->Password = $password; $logInParam->oLogInInfo->UserName = $username; $client = new SoapClient($wsdl, array("trace" => 1)); $logInResponse = $client->LogIn($logInParam); if(isset($logInResponse->LogInResult->ResultCode) && $logInResponse->LogInResult->ResultCode == "OK" && isset($logInResponse->LogInResult->SessioID) && !empty($logInResponse->LogInResult->SessioID)){ $sessionId = $logInResponse->LogInResult->SessioID; } $lastUpdateDate = gmdate("Y-m-dTH:i:s", strtotime("-{$pastDays} days")); $changedProductsParam = new stdClass(); $changedProductsParam->strSessionId = $sessionId; $changedProductsParam->dtLast_actiondate = $lastUpdateDate; $changedProductsResponse = $client->GetWebProductList_Changed_Stock($changedProductsParam); if($changedProductsResponse && isset($changedProductsResponse->GetWebProductList_Changed_StockResult->Product)){ foreach($changedProductsResponse->GetWebProductList_Changed_StockResult->Product as $changedProduct){ var_dump($changedProduct->Product_code); } }