2012-04-23 16 views
1

APIを使用してWikipediaを検索して、自分のフォームを入力したユーザーが入力した単語を検索しようとしています。 APIは単語 "cat"を含むエントリをwikipediaで検索します。私はそれが動作するようになったが、今私は、「このメッセージになっM:。HTTP/1.0 403 Wikipedia APIを使用しているときに禁止されたエラーメッセージ

Warning: file_get_contents(http://en.wikipedia.org/w/api.php?action=opensearch&search=parrott): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /var/www/html/flam1-api.php on line 22 

私はおよそおそらくユーザーエージェントを必要と読んで、私は正確に何をすべきかわからないがここに私のコードです私は間違いなくこの上の任意の助けに感謝

echo "<h1>Which LOLcat are you? Results!</h1>"; 
    $visit_id = $_COOKIE['visit_id']; 
    $all_my_variables = json_decode(file_get_contents("/var/www/html/data/$visit_id.json")); 
    //var_dump($all_my_variables); 
    $animal = $all_my_variables ->favoriteanimal; 
    echo "When searching wikipedia entries on your favorite animal, which is a $animal, we got the results:<br>"; 
    $website = file_get_contents('http://en.wikipedia.org/w/api.php?action=opensearch&search='.urlencode($animal).''); 

     echo $website[0]; 

+0

[WikiMediaユーザーエージェントポリシー](http://meta.wikimedia.org/wiki/User-Agent_policy)を参照してください。 – svick

答えて

3

は、あなたはそれが頻繁に関連付けられていますよう。file_get_contentsが使用するデフォルトのユーザーエージェントが明示的に、ブロックされている(代わりにfile_get_contentscurl拡張子を使用して、おそらく)ユーザーエージェントを設定する必要があります虐待的な行動をする。

1

一部のサイトは、file_get_contentsを使用してブロックします。 私は今、テストするチャンスがありませんが、この関数はfile_get_contentsのisnteadを使ってみてください。

function get_url_contents($url){ 
     $crl = curl_init(); 
     $timeout = 5; 
     curl_setopt ($crl, CURLOPT_URL,$url); 
     curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 
     $ret = curl_exec($crl); 
     curl_close($crl); 
     return $ret; 
} 
+1

問題はユーザーエージェントです。 curlのデフォルトのUser Agentもブロックされていると思いますので、明示的に設定する必要があります。 – svick

関連する問題