试图获得非对象的属性

时间:2011-06-23 19:15:35

标签: php mysql arrays json curl

<?php
include 'includes/config.php';

function do_curl($start_index, $stop_index) {

    // Do query here to get all pages with ids between start index and stop index

    $query = "SELECT * FROM fanpages WHERE idnum >= $start_index and idnum <= $stop_index";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result))
    {
        $fanpages_query[] = 'http://graph.facebook.com/'.$row['page_id'];
    }
    return $fanpages_query;   
}

$fanpages = do_curl($_GET['start_index'], $_GET['stop_index']);
$fanpages_count = count($fanpages);

$curl_arr = array();
$master = curl_multi_init();

for($i = 0; $i < $fanpages_count; $i++)
{
    $url = $fanpages[$i];
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
    curl_multi_exec($master,$running);
} while($running > 0);

echo "results: <br>";
for($i = 0; $i < $fanpages_count; $i++)
{
    $results = json_decode(curl_multi_getcontent($curl_arr[$i]));
    echo("Page Name: ".($results->name)."<br>"."Likes: ".($results->likes)."<br>"."ID: ".($results->id)."<br><br>");
}
echo 'done';


?>

就像你几乎可以看到一切正常,唯一的问题是数组,我问了两个小时之前有关这个的问题,但我得到的答案对我不起作用。所以问题是do_curl和while$fanpages数组列表的组合不起作用。每次我试图运行脚本时,我都会获得正确数量的页面,正确数量的数据,但它会抛出这个错误:

Notice: Trying to get property of non-object in C:\xampp\htdocs\curltest.php on line 46

Notice: Trying to get property of non-object in C:\xampp\htdocs\curltest.php on line 46

Notice: Trying to get property of non-object in C:\xampp\htdocs\curltest.php on line 46

我敢打赌,数组不会像他们需要的那样进行解析$ fanpages = array('http://graph.facebook.com/1111111','http://graph.facebook.com/222222222','http://graph.facebook.com/333333333'); |因为如果我手动编写它们一切正常。

编辑:脚本错误:

 $fanpages_query[] = '\'http://graph.facebook.com/'.$row['page_id']."'";

我刚刚删除了\'而现在它的抛出:

Notice: Undefined property: stdClass::$name in C:\xampp\htdocs\curltest.php on line 48

添加var_dump后:

object(stdClass)#1 (1) { ["error"]=> object(stdClass)#2 (2) { ["type"]=> string(20) "GraphMethodException" ["message"]=> string(24) "Unsupported get request." } } string(78) "{"error":{"type":"GraphMethodException","message":"Unsupported get request."}}" NULL
Notice: Undefined property: stdClass::$name in C:\xampp\htdocs\curltest.php on line 48

1 个答案:

答案 0 :(得分:2)

$results = json_decode(curl_multi_getcontent($curl_arr[$i]));

JSON解码失败。将其更改为此并发布结果,以便我们可以获得更多信息:

$results = json_decode($raw_results = curl_multi_getcontent($curl_arr[$i]));
var_dump($results, $raw_results, !$results?json_last_error():null);

编辑1

根据评论,您没有收到任何数据。您是否有理由不使用curl_multi_exec() manual page中提供的代码?

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}