繁体   English   中英

get_headers php函数抛出错误

[英]get_headers php function throwing error

我正在尝试从URL获取元标题和关键字。

我在excel工作表中有URL列表,首先使用phpExcel库获取URL,然后在foreach循环中运行。 并将结果写在新的Excel工作表中

我的代码如下

<?php
include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
require_once 'PHPExcel/IOFactory.php';

$readFileName = "script_test.xlsx";
$target_file_path = "results.xlsx";

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($readFileName);
$objWorksheet = $objPHPExcel->getActiveSheet();

$objPHPExcel1 = PHPExcel_IOFactory::load($target_file_path);

$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();

$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

$i = 2;
for ($row = 1; $row <= $highestRow; ++$row) 
{
    $keyword_val = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
    $url_headers_details = get_headers($keyword_val, 1);

    if($url_headers_details[0] = "HTTP/1.1 200 OK")
    {
        $html = file_get_contents_curl($keyword_val);

        //parsing begins here:
        $doc = new DOMDocument();
        @$doc->loadHTML($html);
        $nodes = $doc->getElementsByTagName('title');

        //get and display what you need:
        $title = $nodes->item(0)->nodeValue;

        $metas = $doc->getElementsByTagName('meta');

        for ($i = 0; $i < $metas->length; $i++)
        {
            $meta = $metas->item($i);
            if($meta->getAttribute('name') == 'keywords')
                $keywords = $meta->getAttribute('content');
        }

        $objPHPExcel1->getActiveSheet()->setCellValue('A'.$i, $keyword_val);

        if (!isset($title)) {
            $objPHPExcel1->getActiveSheet()->setCellValue('B'.$i, "NA");
        }
        elseif (isset($title)) {
            $objPHPExcel1->getActiveSheet()->setCellValue('B'.$i, $title);
        }

        if (!isset($keywords)) {
            $objPHPExcel1->getActiveSheet()->setCellValue('C'.$i, "NA");
        }
        elseif (isset($keywords)) {
            $objPHPExcel1->getActiveSheet()->setCellValue('C'.$i, $keywords);
        }
        $i++;
    }
}

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
    $objWriter->save($target_file_path);

function file_get_contents_curl($url)
        {
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

            $data = curl_exec($ch);
            curl_close($ch);

            return $data;
        }
?>

这段代码在几个域上都可以正常工作,但是在几个域的情况下,会向我抛出如下错误:

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/Qdrive/test/KinjalG/PHPExcel-develop/Classes/meta_titles_keywords.php on line 31

Warning: get_headers(http://www.mrmeticulous.com.au ): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/Qdrive/test/KinjalG/PHPExcel-develop/Classes/meta_titles_keywords.php on line 31

Notice: Trying to get property of non-object in /opt/lampp/htdocs/Qdrive/test/KinjalG/PHPExcel-develop/Classes/meta_titles_keywords.php on line 45

Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/Qdrive/test/KinjalG/PHPExcel-develop/Classes/meta_titles_keywords.php on line 31

我要去哪里错了?

谢谢

我可以调试自己的自我:)

if condition需要添加if conditionif condition解决方案很简单。 更换

$nodes = $doc->getElementsByTagName('title');

        //get and display what you need:
        $title = $nodes->item(0)->nodeValue;

通过

$nodes = $doc->getElementsByTagName('title');

($nodes->length>0) {
            //get and display what you need:
            $title = $nodes->item(0)->nodeValue;
        }

$nodes = $doc->getElementsByTagName('title'); 将返回找到的结果的长度。 如果为零,则会发出警告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM