failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

My code:

function jsonCall() {      global $_SESSION;     global $smarty;      $client_id = "XXXXXXXX";     $next_max_tag_id = 1349756086082;      if(isset($_POST['nextSubmit']))         {             $next_max_tag_id = $_POST['next_max_tag_id'];         }       $url ='https://api.instagram.com/v1/tags/' . $_SESSION['zoekterm'] . '/media/recent?access_token=145408254.f59def8.448ef83c8fa740aa9744a0c47766a857&max_tag_id=' . $next_max_tag_id;      $json = file_get_contents($url);     $json_output = json_decode($json,true);      file_put_contents('json/instagram.json',$json);      $imagesErbuiten = array();              foreach($json_output['data'] as $data)             {                 foreach($data['images'] as $images)                 {                     array_push($imagesErbuiten, $images);                 }             }       $smarty->assign('arrData',$imagesErbuiten);     $smarty->assign('next_max_tag_id',$json_output['pagination']['next_max_tag_id']); } 

I am working with the Instagram API and Flickr API. Both get same errors when there is a value they don't know in their searchfield.

As an example:

Instagram doesn't allow searches by tag like porn, tits, ... When this happens you get the 400 Bad request. When you search for QSDFQSDFQSDFQSDF (which doesn't exist ether) there is no error, just because the search is valid but the array is empty (which is good, so i can display "Nothing has been found").

It's not a empty space problem. I allready excluded all empty spaces and replaced them with a +.

My question:

Is it possible to just drop or fix this error? Everything works fine, except you get that ugly error at the top of my page.

Greetings, W.

回答1:

One alternative is to use cURL (http://php.net/manual/en/book.curl.php) to get more details about the request before the print:

Some like this maybe can help you:

function url_get_contents ($Url) {     if (!function_exists('curl_init')){          die('CURL is not installed!');     }     $ch = curl_init();     curl_setopt($ch, CURLOPT_URL, $Url);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     $output = curl_exec($ch);     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get the code of request     curl_close($ch);      if($httpCode == 400)         return 'No donuts for you.';      if($httpCode == 200) //is ok?        return $output;   } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!