我试图通过php从twitter api中捕获404status。我试图使用curl但实际上我更喜欢使用带有file_get_contents()的条件语句,因为我不想下载和安装curl库。 但我不知道如何编写代码来获取带有404status返回的错误消息。
$userid = $_GET['userid'];
$url="https://api.twitter.com/1/statuses/user_timeline/$userid.xml?include_rts=true&count=5";
//curl that i don't want to have to use
// would prefer to use something like if(file_get_contents($url) == error)
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if( $header == "404" )
{
echo false;
}
else{
$buffer = file_get_contents($url);
$xml = new SimpleXMLElement($buffer);