Ошибка синтаксического анализа в php-поиске фотографий instagram


У меня есть php-приложение. Мне нужен поиск фотографий в инстраграме по тегу.

Результатом вызова ajax является ошибка: Ошибка синтаксического анализа

Вот источник:

Index.html

<!doctype html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Instagram Photo Instant Search App with jQuery</title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script type="text/javascript" src="ajax.js"></script>
</head>

<body>
    <div id="w">
        <section id="sform">
            <small>Note: No spaces or punctuation allowed. Searches are limited to one(1) keyword.</small>
            <input type="text" id="s" name="s" class="sfield" placeholder="Enter a search tag..." autocomplete="off">
        </section>

        <section id="photos"></section>
    </div>
</body>
</html>

Ajax.js

$(document).ready(function(){   
var sfield = $("#s");   
var container =$("#photos");    
var timer;      
function instaSearch() {
$(sfield).addClass("loading");      
$(container).empty();       
var q =$(sfield).val();

$.ajax({            
type: 'POST',           
url: 'instasearch.php',                                 
data: "q="+q,           
success: function(data){
$(sfield).removeClass("loading");               
$.each(data, function(i, item) {
var ncode = '<div class="p"><a href="'+data[i].src+'" class="fullsize" target="_blank"><img src="img/full-image.png" alt="fullsize"></a> <a href="'+data[i].url+'" target="_blank"><img src="'+data[i].thumb+'"></a></div>';

$(container).append(ncode);
});
},          
error: function(xhr, type, exception) { 
alert(exception);
alert(xhr.responseText);    
                                alert(type);
                                if (status === 'error' || !xhr.responseText) 
                                {
                                   alert("podrido");
                                   handleError();
                             }                            
        $(sfield).removeClass("loading");
$(container).html("Error: " + type);            }       });     }       /**      * keycode glossary      * 32 = SPACE    * 188 = COMMA   * 189 = DASH    *
190 = PERIOD     * 191 = BACKSLASH   * 13 = ENTER    * 219 = LEFT BRACKET
 * 220 = FORWARD SLASH   * 221 = RIGHT BRACKET   */
$(sfield).keydown(function(e){
 if(e.keyCode == '32' || e.keyCode == '188' || e.keyCode == '189' || e.keyCode == '13' || e.keyCode == '190' || e.keyCode == '219' ||
 e.keyCode == '221' || e.keyCode == '191' || e.keyCode == '220') {
 e.preventDefault();
 else {
clearTimeout(timer);

timer = setTimeout(function() {
 instaSearch();             }, 900);   
}
});

});

Instasearch.php

<?php
header('Content-type: application/json');

$client = "d10b95cf56094bca8b841734baadc367";
$query = $_POST['q'];
$clnum = mt_rand(1,3);

$api = "https://api.instagram.com/v1/tags/".$query."/media/recent?client_id=".$client;


function get_curl($url) {
    if(function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );        
        $output = curl_exec($ch);
        echo curl_error($ch);
        curl_close($ch);
        return $output;
    } else{
        return file_get_contents($url);
    }
}

$response = get_curl($api);
echo $response;
$images = array();

if($response){
    foreach(json_decode($response)->data as $item){     
        $src = $item->images->standard_resolution->url;
        $thumb = $item->images->thumbnail->url;
        $url = $item->link;

        $images[] = array(
        "src" => htmlspecialchars($src),
        "thumb" => htmlspecialchars($thumb),
        "url" => htmlspecialchars($url)
        );

    }
}

print_r(str_replace('\\/', '/', json_encode($images)));
die();
?>

Мне нужно найти фотографии в instagram по тегу. Но запрос ajax возвращает ошибку синтаксического анализа и неперехваченную ошибку синтаксиса: Неожиданный токен:"

Я надеюсь, что вы сможете мне помочь

Большое спасибо

Author: Litox, 2013-11-22

1 answers

Попробуйте добавить:

dataType:'JSON'

На ваш вызов $.ajax, например

$.ajax({            
dataType:'JSON',
type: 'POST',
...
 1
Author: Chris Wheeler, 2013-11-22 16:16:44