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

My code is:

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 Instagram API and Flickr API. Both get the same error when there is a value that they do not know in their search field.

As an example:

Instagram does not allow searches by tags like porn, boobs, ... When this happens, you will receive 400 Bad requests. When you search QSDFQSDFQSDFQSDF (which ether does not exist), there is no error, just because the search is valid, but the array is empty (this is good, so I can display "Nothing was found").

This is not a problem with empty space. I allready excluded all empty spaces and replaced them with +.

My question is:

Can I just remove or fix this error? Everything works fine, except that you get this ugly error at the top of my page.

Hi Tue

+1
json php request
source share
1 answer

One option is to use cURL (http://php.net/manual/en/book.curl.php) to get more detailed information about the request before printing:

Some, for example, may 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; } 
+3
source

All Articles