gpt4 book ai didi

php - php 搜索 instagram 图片时出现 parseError

转载 作者:行者123 更新时间:2023-12-01 04:50:59 41 4
gpt4 key购买 nike

我有一个 PHP 应用程序。我需要按标签搜索 Instagram 图片。

ajax调用的结果是Error:parseError

来源如下:

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请求返回parseError和Uncaught SyntaxError: Unexpected token :"

希望你能帮助我

非常感谢

最佳答案

尝试添加:

dataType:'JSON'

到您的 $.ajax 调用,例如

$.ajax({            
dataType:'JSON',
type: 'POST',
...

关于php - php 搜索 instagram 图片时出现 parseError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20148598/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com