Ignoring errors in file_get_contents HTTP wrapper?
下面的代码是查询我作为大学项目创建的搜索引擎的在线同义词库,但我遇到了
1 2 | $thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php"; $result_thesaurus=file_get_contents($thesaurus_search); |
我试过:
1 2 |
…但它不起作用,因为它仍然返回某种字符串。
我该怎么办?
如果您不希望
1 2 3 4 5 | $context = stream_context_create(array( 'http' => array('ignore_errors' => true), )); $result = file_get_contents('http://your/url', false, $context); |
最简单的解决办法是,如果你能接受,只是纾困,将是:
1 2 3 4 5 |
为了更全面地处理它,查看API,您应该检查响应头,例如:
1 2 3 4 5 6 7 | $thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php"; $result_thesaurus=file_get_contents($thesaurus_search); if ($http_response_header[0] = 'HTTP/1.1 200 OK') { //code to handle words } else { // do something else? } |
如果我正确理解您的意思,您正试图对
1 2 3 4 5 6 7 8 9 | $ch = curl_init(); $thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php"; $options = array(); $options[CURLOPT_URL] = $thesaurus_search; $options[CURLOPT_RETURNTRANSFER] = true; curl_setopt_array($ch, $options); // Print result. print_r(curl_close($ch)); |
你可以试试卷发:
1 2 3 4 5 6 7 8 9 10 11 | function curl_get_contents($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"); curl_setopt($ch, CURLOPT_MAXREDIRS, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); return $content; } |