2011-09-19 80 views
8

我已經做了一個Twitter應用程序,只需從我的網站發佈推文,使用「twitteroauth」和「oauth」PHP腳本here爲什麼我的Twitter應用程序返回端口錯誤?

一切正常,但我的錯誤日誌給我這個錯誤:

Undefined index: port OAuth.php 383

雖然這似乎並沒有從功能抑制我的劇本,我想繼續我的錯誤日誌無噪音。並避免可能的未來問題。

任何想法爲什麼會發生這種情況?

對於參考OAuth.php代碼中的錯誤日誌指出的是:

public function get_normalized_http_url() { 
$parts = parse_url($this->http_url); 
$port = @$parts['port'];  <-- Line 383 
$scheme = $parts['scheme']; 
$host = $parts['host']; 
$path = @$parts['path']; 
$port or $port = ($scheme == 'https') ? '443' : '80'; 
if (($scheme == 'https' && $port != '443') 
|| ($scheme == 'http' && $port != '80')) { 
$host = "$host:$port"; 
} 
return "$scheme://$host$path"; 
} 
+0

那麼,是什麼'$零件[ 「端口」]'包含哪些內容?我不知道圖書館,但如果它是空的,也許你需要添加一個值? –

回答

7

這是OAuth.php文件中的錯誤,它訪問數組的索引而不執行索引檢查。

很明顯,編寫這個代碼的人很聰明,可以使用錯誤抑制運算符@而不是進行適當的索引檢查 - 懶惰(假設最好)。

報告一個錯誤的上游,一個解決方法是簡單的:

$parts = parse_url($this->http_url) + array('port'=>NULL, 'path'=>NULL); 

和除去兩個@運算符。

5

這是因爲parse_url()不保證返回的端口號(重點煤礦):

If the component parameter is omitted, an associative array is returned. At least one element will be present within the array.

如果您不想觸摸您的error_reporting,請嘗試使用類似$port = (array_key_exists('port', $parts) ? $parts['port'] : 80);的內容隱藏通知。

相關問題