第14行的错误,php curl dom [英] Error on line 14, php curl dom

查看:141
本文介绍了第14行的错误,php curl dom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
$url='http://edition.cnn.com/?fbid=4OofUbASN5k';

$var = fread_url($url);// function calling to get the page from curl
$search = array('@<script[^>]*?>.*?</script>@si');  // Strip out javascript
$var = preg_replace($search, "\n", html_entity_decode($var)); // Strip out javascript

$linklabel = array();
$link = array();
$dom = new DOMDocument($var);
@$dom->loadHTML($var);
$xpath = new DOMXPath($dom);// Grab the DOM nodes 

foreach($xpath->find('a') as $element) {
    array_push($linklabel, $element->innerText);
    print $linklabel;
    array_push($link, $element->href);
    print $link.'<br>';
}


function fread_url($url) {
    if(function_exists("curl_init")) {
        $ch = curl_init();
        $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
                "Windows NT 5.0)";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
        curl_setopt( $ch, CURLOPT_URL, $url );

        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        $html = curl_exec($ch);
//print $html;//printing the web page.
        curl_close($ch);
    }
    else {
        $hfile = fopen($url,"r");
        if($hfile) {
            while(!feof($hfile)) {
                $html.=fgets($hfile,1024);
            }
        }
    }
    return $html;
}



我需要将链接和链接标签分成两个独立的数组。我跟着几个论坛,并提出了一个代码,但是得到错误。我不知道在代码中使用的find函数

i need to seperate links and link labels into two seperate arrays. i followed several forums and made a code, but is getting error. i don't know about the find function used in the code

推荐答案

几个问题,主要是调用不存在的函数和引用不存在属性。正确的版本:

Several problems, mainly calls to inexistent functions and references to inexistent properties. Correct version:

<?php
$var = <<<EOD
<html>
<a href="sdfgs">sdfd</a>
</html>
EOD;

$dom = new DOMDocument();
@$dom->loadHTML($var);
$xpath = new DOMXPath($dom);

foreach($xpath->query('//a') as $element) {
     $linklabel[] = $element->textContent;
     $link[] = $element->getAttribute("href");
}
var_dump($linklabel);
var_dump($link);

这篇关于第14行的错误,php curl dom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆