2010-08-21 20 views
13

Sto cercando di utilizzare file_get_contents() per afferrare un feed Twitter, ma sto ottenendo il seguente avviso:file_get_contents() non è riuscito ad aprire flusso:

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request 

Il mio codice:

$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name=google&count=6'; 
$tweets = file_get_contents($feed); 

Sto usando Google solo per motivi di test. allow_url_fopen è abilitato nel mio file php.ini.

Qualche idea di cosa potrebbe essere sbagliato?

risposta

32

È necessario utilizzare l'estensione PHP cURL se disponibile, poiché è molte volte più veloce, più potente e più facile da eseguire il debug. Ecco un esempio che fa la stessa cosa che si stavano cercando:

function curl($url) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    $data = curl_exec($ch); 
    curl_close($ch); 

    return $data; 
} 

$feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name=google&count=6'; 
$tweets = curl($feed); 
+0

E 'anche molto più facile da mettere a punto, dal momento che si può ottenere per scaricare la richiesta e le intestazioni si invia (le richieste malformate essendo la causa usuale di HTTP 400 errori) –

+1

http://stackoverflow.com/questions/ 555523/file-get-contents-vs-curl-what-has-better-performance –

+0

Sì, circa 4-5 volte più veloce. –

0

Molto probabilmente, il server ha mod_security abilitato. Aggiungi le linee sottostanti nel tuo .htaccess dopo ogni regola esistente e poi prova a vedere il problema è ancora lì, dopo quell'aggiunta.

<IfModule mod_security.c> 
SecFilterScanPost 
</IfModule> 
Problemi correlati