2011-03-17 114 views
1

我正在爲一個電視wordpress網站(www.canal-en-vivo.com)工作幾個php函數,它根據頻道Feed是直播還是根據頻道Feed改變了發佈到草稿的帖子(即頻道)的狀態,或者不。我把以下兩個函數放在一起,但它們似乎不起作用。你們可以看看通用的代碼結構,並告訴我,如果你看到結構有點奇怪嗎?這些功能有什麼問題?

該功能確定飼料是否是通過檢查URL直播或不是「$ feedurlstr」

// This function determines whether a given post/channel is live or not 
// based on whether url $feedurlstr has $string in it. 

    function LiveOrNot($feedurlstr) { 
     global $result; 
     // create curl resource 
     $ch = curl_init(); 
     // set url 
     curl_setopt($ch, CURLOPT_URL, $feedurlstr); 
     //return the transfer as a string 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     // $output contains the output string 
     $output = curl_exec($ch); 
     // close curl resource to free up system resources 
     curl_close($ch);  

     $string = "PP['channel_live'] = true"; 
     if(strstr($output,$string)) { 
     $result = "live"; 
     } else { 
     $result = "notlive"; 
     } 
     return $result; 
    } 

而這個函數運行的SQL查詢其改變狀態基於先前的功能是否返回$每個崗位結果是活的或不活潑的。

// Function runs SQL query based on channel status $result 

    function RunChannelLiveQuery() { 
     global $key; 
     global $post_ids; 
     $key = 'feed'; 
     // This array includes the Post ID to be checked 
     $post_ids = array(2263, 2249); 

     foreach ($post_ids as $id) { 
     // Wordpress function to pull value out of a "custom field" - spits URL 
      $feedurl = get_post_custom_values($key, $id); 
      // turns $feedurl into string 
      $feedurlstr = implode($feedurl); 
      // Find whether feed is live or not 
      LiveOrNot($feedurlstr); 

      // Performs DB Query based on live or not 
      if ($result == "live") { 
       mysql_query ("UPDATE 'wp_posts' SET 'post_status' = 'publish' WHERE 'post_id' = '$id'") or die(mysql_error()); 
      } 
      if ($result == "notlive") { 
       mysql_query ("UPDATE 'wp_posts' SET 'post_status' = 'draft' WHERE 'post_id' = '$id'") or die(mysql_error()); 
      } 
     } 
    } 

關於這些功能可能出錯的任何想法幫派?

回答

0

嘗試這種情況:

LiveOrNot()函數刪除行

global $result; 

而在RunChannelLiveQuery(),取代

LiveOrNot($feedurlstr); 

通過

$result = LiveOrNot($feedurlstr); 
+0

冷若冰霜,感謝您的幫助!我做了更改,但我收到以下SQL錯誤:'您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在''wp_posts'SET'post_status'='draft'WHERE'post_id'='2263''第1行附近使用正確的語法任何想法? – Alberto 2011-03-17 20:20:58

+0

您不應該在表名和字段名稱周圍添加單引號。只有使用反引號('')是可能的。然後,你的第一個查詢字符串應該看起來像「」UPDATE wp_posts SET post_status ='publish'WHERE post_id ='$ id'「'。查看MySQL手冊獲取更多細節。 – 2011-03-17 20:49:53

+0

這是正確的冷霜。現在它聲稱DB中沒有post_id列'Unknown'列'post_id'in'子句'Odd ... – Alberto 2011-03-17 20:54:59

1

雖然這不是必需的,但您可能希望使用Wordpress API來更新發布狀態

+0

我實際上遇到了SQL查詢的一些問題:'你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在''wp_posts'SET'post_status'='draft'WHERE'post_id'='2263''第1行附近使用正確的語法。使用Wordpress API ... – Alberto 2011-03-17 20:49:56

+0

是的,絕對是一個很好的建議。 – 2011-03-17 21:11:17