1

我的代码:

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']);
}

我正在使用 Instagram API 和 Flickr API。当他们的搜索字段中有一个他们不知道的值时,两者都会得到相同的错误。

举个例子:

Instagram 不允许按色情、山雀等标签进行搜索……当这种情况发生时,您会收到 400 Bad 请求。当您搜索 QSDFQSDFQSDFQSDF (不存在以太)时,没有错误,只是因为搜索有效但数组为空(这很好,所以我可以显示“未找到任何内容”)。

这不是空间问题。我已经排除了所有空格并用 + 替换它们。

我的问题:

是否可以删除或修复此错误?一切正常,除了你在我的页面顶部遇到那个丑陋的错误。

问候,W.

4

1 回答 1

4

一种替代方法是使用 cURL (http://php.net/manual/en/book.curl.php) 在打印之前获取有关请求的更多详细信息:

像这样的一些可能可以帮助你:

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;


}
于 2012-10-11T12:26:39.590 回答