首页 > 解决方案 > 从 html 格式 PHP 中的 Curl 响应中获取数据

问题描述

我正在使用 Curl 更新一些记录,在发送响应请求后,我得到了 HTML 格式数据。我想检查响应,响应是什么。我的代码是:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
        $cacheManager->clean('catalog_product_' . $param);
        $varnishurl = "www.exapmle.com";
        $varnishcommand = "PURGE";
        $productID = $param; // This is the Magento ProductID of the item you want to purge 

        $curl = curl_init($varnishurl);
        curl_setopt($curl, CURLOPT_POST, 1);        
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $varnishcommand);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-Magento-Tags-Pattern: catalog_product_'.$productID]);

        $result = curl_exec($curl);     
        curl_close($curl);

OutPut: 在此处输入图像描述 我想添加条件来检查返回的内容?要么它像响应标题或正文文本一样被清除。我已经尝试通过 json 格式发送但没有运气。

标签: phpcurlcachingmagento2

解决方案


只需稍微更改我的代码,我就得到了答案。添加带标签标签以删除 HTML 标签,然后搜索所需的内容。

$data = strip_tags($result);
    if(strpos($data, '200 Purged')  !== false){     
    return 'Cache updated successfully for the Product Id '.$param;
   }else{
      return 'Some error occured during cache update for the Product Id '.$param;
   }

推荐阅读