dpoxk64080 2017-08-25 09:21
浏览 38

cURL请求.ATOM Feed问题

This is my XML format .ATOM feed. I am getting the following WARNINGS.

I am facing issues trying to extract the data from the ATOM URL and display it as a PHP code and save it to the DATABASE. I keep getting these errors...

I have tried using loadfile, curl, vardump() and nothing worked.

Warning: simplexml_load_file(https://colourpop.com/collections/all.atom): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /public_html/ecomcharts/beta/indexproducts.php on line 226

Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://colourpop.com/collections/all.atom" in /public_html/ecomcharts/beta/indexproducts.php on line 226 bool(false)

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /public_html/ecomcharts/beta/indexproducts.php on line 228

Warning: SimpleXMLElement::__construct(): https://colourpop.com/collections/all.atom in //ecomcharts/beta/indexproducts.php on line 228

I am not sure what is wrong. This is my PHP code:

<?php
    $url = 'https://colourpop.com/collections/all.atom';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $data = curl_exec($curl);
    curl_close($curl);

    function download_page($path) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);          
        curl_close($ch);
        return $retValue;
    }

    $sXML = download_page('https://colourpop.com/collections/all.atom');
    echo $sXML;
    $xml=var_dump(simplexml_load_file('https://colourpop.com/collections/all.atom'));

    $oXML = new SimpleXMLElement('https://colourpop.com/collections/all.atom');

    foreach($oXML->entry as $oEntry) {
        echo $oEntry->title . "
";
    }
?>
  • 写回答

1条回答 默认 最新

  • douzoudang1511 2017-08-25 14:36
    关注

    When creating a SimpleXMLElement with the constructor, you pass in the contents of the file and not the file name, so in your case...

    <?php
    $url = 'https://colourpop.com/collections/all.atom';
    $options = array(
            CURLOPT_RETURNTRANSFER => true,     // return web page
            CURLOPT_HEADER         => false,    // don't return headers
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
            CURLOPT_ENCODING       => "",       // handle all encodings
            CURLOPT_USERAGENT      => "spider", // who am i
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
            CURLOPT_TIMEOUT        => 120,      // timeout on response
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
            CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
    );
    
    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $sXML = curl_exec( $ch );
    curl_close( $ch );
    
    $oXML = new SimpleXMLElement ( $sXML );
    
    foreach ( $oXML->entry as $oEntry ) {
        echo $oEntry->title . "
    ";
    }
    

    This also fixes the problems of trying to read HTTPS pages using curl (using the CURLOPT_SSL_VERIFIER method)

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?