gpt4 book ai didi

php - 使用 PHP 解析 JSON 结果 - Yahoo Search API

转载 作者:可可西里 更新时间:2023-11-01 13:30:38 26 4
gpt4 key购买 nike

我可以使用我的 API key 从 yahoo 检索结果,使用 yahoo 开发者网站上的说明。 http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#

代码:

if ($_POST['query'])
{
$newline="<br />";
$query = urlencode("'{$_POST['query']}'");

require("OAuth.php");

$cc_key = "key goes here";
$cc_secret = "secret goes here";
$url = "http://yboss.yahooapis.com/ysearch/web";
$args = array();
$args["q"] = "$query";
$args["format"] = "json";

$consumer = new OAuthConsumer($cc_key, $cc_secret);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
$ch = curl_init();
$headers = array($request->to_header());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$rsp = curl_exec($ch);
$results = json_decode($rsp);

print_r($results);

}

如上所示使用 print_r($results),我得到如下结果(搜索“elephant 时显示的前三个结果的摘录) "):

请注意,我已将 URL 更改为“WWW”,因为我需要至少 10 个信誉才能发布超过 2 个链接。

stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [web] => stdClass Object ( [start] => 0 [count] => 50 [totalresults] => 36800000 [results] => Array ( [0] => stdClass Object ( [date] => [clickurl] => WWW [url] => WWW [dispurl] => en.wikipedia.org/wiki/Elephant [title] => Elephant - Wikipedia, the free encyclopedia [abstract] => Elephant trunks have multiple functions, including breathing, olfaction, ... One elephant has been observed to graze by kneeling on its front legs, ... ) [1] => stdClass Object ( [date] => [clickurl] => WWW [url] => WWW [dispurl] => www.defenders.org/elephant/basic-facts [title] => Elephant | Basic Facts About Elephants | Defenders of Wildlife [abstract] => Elephant. Basic Facts About Elephants More on Elephant: Threats to Elephants » More on Elephant: Basic Facts . Threats. What Defenders Is Doing to Help. What You Can ... ) [2] => stdClass Object ( [date] => [clickurl] => WWW [url] => WWW [dispurl] => kids.nationalgeographic.com/.../african-elephant [title] => African Elephant Facts and Pictures -- National Geographic Kids [abstract] => Kids' feature about elephants, with photographs, video, audio, fun facts, an e-mail postcard, and links to other animals. ) [3] => stdClass Object ( [date] => [clickurl] => WWW [url] => WWW [dispurl] => elephant.elehost.com/About_Elephants/about_elephants.htm [title] => About Elephants [abstract] => All about elephants on the Elephant Information Repository! This page includes a summary of elephant related facts to get you inducted in to the world of elephants. )

我尝试以清晰的格式输出结果,如下所示:

代码尝试 1:

foreach ($results->{ 'results' } as $item ) 
{

echo "<a href=\"{$item->{ 'url' }}\"><font color ='blue'>{$item->{ 'title' }}</font></a>".": "."$newline"."$newline".$item->{ 'abstract' }."\n\n";


}

我也尝试了以下,但没有成功:

代码尝试 2:

echo $results['results']['url'];
echo $results['results']['title'];
echo $results['results']['abstract'];

有什么想法吗?

谢谢。

最佳答案

我注意到您只是复制粘贴了 the documentation's code examples 中的代码,但没关系。

您正在以错误的方式访问 results 数组:

foreach ($results->bossresponse->web->results as $result)
{
//do stuff
echo $result->title.'<br/>';
}

或者,如 cptnk 所建议的那样:

$results = json_decode($rsp, true);
//force to assoc-array, which will allow array-access
foreach($results['bossresponse']['web']['results'] as $result)
{
//$result is array here, but do the same stuff
echo $result['title'].'<br/>';
}

或者,将两者结合起来

foreach($results->bossresponse->web->results as $result)
{
$result = (array) $result;//casts stdClass to array
printf('<a href="%s">%s</a><br/>', $result['url'], $result['title']);
}

关于php - 使用 PHP 解析 JSON 结果 - Yahoo Search API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17400966/

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