【问题标题】:Did not getting data from URL using cUrl?没有使用 cUrl 从 URL 获取数据?
【发布时间】:2015-02-01 14:18:28
【问题描述】:
<?php
    $con=mysql_connect("localhost","root","");
    mysql_select_db("google",$con);

    $sql="SELECT urlname FROM url WHERE id=12";
    $url=mysql_query($sql);
    $result = get_web_page( $url );

    if ( $result['errno'] != 0 ) {
        echo "errror";
    }

    if ( $result['errmsg'] != 200 ) {
        echo "error";
    }

    $page = $result['content'];
    while ($row = mysql_fetch_array($page)) {
        printf($row[0]);  
    }

    function get_web_page( $url1 )
    {
        $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
        );

        $ch = curl_init( $url1 );
        // create a new cURL resource
        //$ch    = curl_init();

        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL,$url1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        $header['errno']   = $err;
        $header['errmsg']  = $errmsg;
        $header['content'] = $content;
        return $header;
    }
?>                      

这是我的 php 代码,当我回显 $page 时,我收到以下警告和错误,并且没有从请求的 url 获得任何数据。

警告:

警告:curl_init() 期望参数 1 是字符串,给定资源 在 C:\xampp\htdocs\CSE391\curl.php 第 36 行

警告:curl_setopt_array():提供的参数不是有效的 cURL 在第 37 行处理 C:\xampp\htdocs\CSE391\curl.php 中的资源

警告:curl_exec() 期望参数 1 是资源,在 C:\xampp\htdocs\CSE391\curl.php 在第 38 行

警告:curl_errno() 期望参数 1 是资源,给定 null 在 C:\xampp\htdocs\CSE391\curl.php 第 39 行

警告:curl_error() 期望参数 1 是资源,给定 null 在 C:\xampp\htdocs\CSE391\curl.php 第 40 行

警告:curl_getinfo() 期望参数 1 是资源,给定 null 在 C:\xampp\htdocs\CSE391\curl.php 第 41 行

警告:curl_close() 期望参数 1 是资源,给定 null 在 C:\xampp\htdocs\CSE391\curl.php 第 42 行错误

【问题讨论】:

  • 我认为你应该在 PHP 中启用 cURL,看到这个问题:stackoverflow.com/questions/1347146/… 另外:使用 mysqli 或 PDO
  • 实际上我启用它但得到相同的结果
  • 你需要获取你的 SQL 查询结果。现在你只是传递一个资源。不是价值

标签: php curl web-crawler


【解决方案1】:

mysql_query 返回一个结果集对象,而不是结果数据。你需要例如fetch_array() 来自结果集对象的每个结果行;该结果行将具有 url

http://php.net/manual/en/function.mysql-fetch-array.php

【讨论】:

  • 是的,我这样做了,但仍然有警告和错误
  • 如果您这样做了,请编辑您的问题以显示您正在运行的代码。我们只能调试我们能看到的代码。
  • 这是正确的。 OP 没有从查询结果中获取数据。此刻它只是一个资源
猜你喜欢
  • 2019-10-25
  • 1970-01-01
  • 2018-03-07
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
相关资源
最近更新 更多