2012-08-12 60 views
0

我需要從mysql數據庫張貼多個消息到facebook牆。首先我從mysql獲取數據並將其放在while循環中多個facebook張貼使用curl

while($row=mysql_fetch_array($result)) 
     { 

$des=$row[1]; 
$purpose=$row[3]; 
$price_sale=$row[4]; 
$price_rent=$row[5]; 
$img="example.com/images".mysql_result($result,0,2); 


    $attachment = array(
    'access_token' => "$token", 
    'message' => $des, 
    'picture' => $img, 
    'link' => "example.com" 
    ); 

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed'); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 


echo $result; 

} 

$ result包含3條記錄。但只發布第一行。 Plz爲此提供了一個解決方案

+0

將'mysql_result()'指定給一個變量並將其移出循環,沒有理由爲每次迭代調用它。並停止使用mysql擴展,因爲它已被棄用。看看[PDO](http://php.net/manual/en/book.pdo.php)或[MySQLi](http://php.net/manual/ru/book.mysqli.php)擴展。 – mintobit 2012-08-12 11:16:05

回答

1

嘗試更改接受curl輸出的變量的名稱。您使用的是上述相同的變量。

while($row=mysql_fetch_array($result)) 
      { 

    $des=$row[1]; 
    $purpose=$row[3]; 
    $price_sale=$row[4]; 
    $price_rent=$row[5]; 
    $img="example.com/images".mysql_result($result,0,2); 


     $attachment = array(
     'access_token' => "$token", 
     'message' => $des, 
     'picture' => $img, 
     'link' => "example.com" 
     ); 

    $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed'); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
    $curlresult = curl_exec($ch); 
    curl_close ($ch); 


    echo $curlresult; 

    } 
+1

oops。我錯過了那個變量。 – 2012-08-12 10:02:45