简体   繁体   中英

Twitter API - 406 Not Acceptable Response

For a user profile i query the Twitter Search API via:

curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.rss?q=".urlencode($username."+-badword")."");

which worked excellent the last weeks.

Since a few days the Twitter API gets a lot of requests from my site and sometimes responses with a "406 Not Acceptable"

the complete error message is:

array(18) { [0]=> string(27) "HTTP/1.1 406 Not Acceptable" [1]=> string(35) "Date: Fri, 21 Jan 2011 12:31:06 GMT" [2]=> string(10) "Server: hi" [3]=> string(26) "Status: 406 Not Acceptable" [4]=> string(15) "Retry-After: 10" [5]=> string(30) "X-Served-From: slc1-aah-25-sr1" [6]=> string(38) "Content-Type: text/html; charset=utf-8" [7]=> string(45) "X-Served-By: slc1-acm-32-sr1.prod.twitter.com" [8]=> string(55) "Cache-Control: max-age=15, must-revalidate, max-age=300" [9]=> string(38) "Expires: Fri, 21 Jan 2011 12:36:06 GMT" [10]=> string(21) "Vary: Accept-Encoding" [11]=> string(17) "Content-Length: 1" [12]=> string(21) "X-Varnish: 1897557299" [13]=> string(6) "Age: 0" [14]=> string(16) "Via: 1.1 varnish" [15]=> string(45) "X-Cache-Svr: slc1-acm-32-sr1.prod.twitter.com" [16]=> string(13) "X-Cache: MISS" [17]=> string(17) "Connection: close" }

I don't understand the Twitter Doku about this:

http://apiwiki.twitter.com/w/page/22554652/HTTP-Response-Codes-and-Errors says "406 Not Acceptable: Returned by the Search API when an invalid format is specified in the request."

because, when i retry after a few seconds the request works fine again.

Who has an idea how to fix this?

Reason:
From the docs , the supported formats are:

1) json
2) atom

And you're using rss . That's why you're getting the error. The format has to be specified as the extension. Like this:

http://search.twitter.com/search.format

Solution:
Use atom or json format, like this:

http://search.twitter.com/search.json?q=twitter

Full code should look like:

curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.json?q=".urlencode($username."+-badword")."");

The Twitter API returns a whole series of 400 and 500 errors randomly and at a low level. Generally the exact error code is meaningless, since it varies. From my experience a false error happens about 0.1% of the time when you make a request. The only solution is to repeat the request.

If the error is reproducible, check your code. If the error is random and intermittent, write code that repeats the request when an error occurs.

If it gets overloaded, that might be the reason for the error.

What does 'badword' contain? If it's variable it might contain something that is incorrectly encoded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM