【发布时间】:2021-02-07 15:00:12
【问题描述】:
我有一个 api 调用,其中包含在返回的 JSON 中调用的 Url。
我需要找到那些返回的 url,其中包含用于原始 get 请求的字符串并且状态为 200。我尝试使用 strpos 方法,但这似乎不起作用。
public function getCompanyData() {
$client = new Client();
$response = $client->request('GET', 'https://api.github.com/orgs/fakename');
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
$confirmed = strpos($body,'api.github.com/orgs/fakename');
if($statusCode === 200 && $confirmed !== false) {
return $body;
}
else {
return $statusCode." Error";
}
}
返回的 JSON -
{
"login": "fakename",
"id": 1214096,
"node_id": "***=",
"url": "https://api.github.com/orgs/fakename",
"repos_url": "https://api.github.com/orgs/fakename/repos",
"events_url": "https://api.github.com/orgs/fakename/events",
"hooks_url": "https://api.github.com/orgs/fakename/hooks",
"issues_url": "https://api.github.com/orgs/fakename/issues",
"members_url": "https://api.github.com/orgs/fakename/members{/member}",
"public_members_url": "https://api.github.com/orgs/fakename/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/****?v=4",
"description": "",
"name": "fakename",
"company": null,
"blog": "fakename.com",
"location": null,
"email": null,
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 41,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/fakename",
"created_at": "2011-11-22T21:48:43Z",
"updated_at": "2020-09-11T23:05:04Z"
}
【问题讨论】:
-
您需要在 PHP 或 javascript 中执行此操作吗?
-
另外,这个JSON是你调用
$client->request('GET', 'https://api.github.com/orgs/fakename');得到的值,还是getCompanyData返回的值? -
@WesleySmith 这个获取请求在我的控制器中,所以在 php 中。但我最终会将数据调用到客户端。 JSON 来自 getCompanyData 返回。当我简单地返回 $body 时,这是我返回的 JSON
-
一旦我识别出状态为 200 的匹配 JSON url,我将打印出其中的键和值
-
我猜我很困惑,因为您正在调用
$client->request('GET', 'https://api.github.com/orgs/fakename');,这似乎返回了一个 json 值,其中url值是被调用的 url,这似乎表明如果您检查了 @987654327 @(被调用的 url)检查将总是返回 true。我在这里错过了什么?