如何将其写入PHP代码?图像的Alt

时间:2017-10-29 14:57:43

标签: php xpath xpath-2.0

如何将此代码写入代码?

如果图像的alt是“你的alt在这里”,则为1。

这是我制作的代码。

public function getSiteAdvisor($domain)
        {
            try
            {
                $callback_url = "https://www.mcafee.com/threat-intelligence/site/default.aspx?url=" . $domain; 

            $curl_response = $this->curl->get($callback_url);

            if ($curl_response->headers['Status-Code'] == "200") {

                libxml_use_internal_errors(TRUE);
                $this->dom_doc->loadHTML($curl_response);
                libxml_use_internal_errors(FALSE);

                $xpath = new DOMXPath($this->dom_doc);
                $tmp = $xpath->query('/html/body//div[@class="threeFourth"]//img/@alt')->item(0);

I don't know how to code the 'alt' of the image. I add some code but not sure if this is correct.

if ($tmp->hasAttribute('src')) {

$tmp = $tmp->getAttribute('src'); if (stripos($tmp, "Minimal") !== false) { $siteadvisor_rating = 1; } elseif (stripos($tmp, "Moderate") !== false) { $siteadvisor_rating = 2; } elseif (stripos($tmp, "High") !== false) { $siteadvisor_rating = 3; } else { $siteadvisor_rating = 0; } } else { $siteadvisor_rating = 0; } } else { $siteadvisor_rating = 0; } $response = array( 'status' => 'success', 'data' => array( 'siteadvisor' => (int)$siteadvisor_rating ) ); } catch (Exception $e) { $response = array( 'status' => 'error', 'msg' => $e->getMessage() ); } return $response; }

感谢

1 个答案:

答案 0 :(得分:0)

我用这个找到答案:

 if ($tmp->hasAttribute('src')) {

$tmp = $tmp->getAttribute('alt'); if (stripos($tmp, "Minimal") !== false) { $siteadvisor_rating = 1; } elseif (stripos($tmp, "Moderate") !== false) { $siteadvisor_rating = 2; } elseif (stripos($tmp, "High") !== false) { $siteadvisor_rating = 3; } else { $siteadvisor_rating = 0; } } else { $siteadvisor_rating = 0; } } else { $siteadvisor_rating = 0;