int(0) ["results-6ren">
gpt4 book ai didi

json - import.io JSON 数据

转载 作者:行者123 更新时间:2023-12-01 12:36:14 25 4
gpt4 key购买 nike

我需要从 import.io 生成的 JSON 中输出一些值

这是 JSON:

object(stdClass)#1 (7) {
["offset"]=>
int(0)
["results"]=>
array(5) {
[0]=>
object(stdClass)#2 (3) {
["vanzare"]=>
string(6) "4.4400"
["moneda"]=>
string(3) "EUR"
["cumparare"]=>
string(6) "4.3550"
}
[1]=>
object(stdClass)#3 (3) {
["vanzare"]=>
string(6) "4.1882"
["moneda"]=>
string(3) "USD"
["cumparare"]=>
string(6) "4.0582"
}
[2]=>
object(stdClass)#4 (3) {
["vanzare"]=>
string(6) "6.1327"
["moneda"]=>
string(3) "GBP"
["cumparare"]=>
string(6) "5.9827"
}
[3]=>
object(stdClass)#5 (3) {
["vanzare"]=>
string(6) "4.2954"
["moneda"]=>
string(3) "CHF"
["cumparare"]=>
string(6) "4.1454"
}
[4]=>
object(stdClass)#6 (1) {
["moneda"]=>
string(3) "EUR"
}
}
["cookies"]=>
array(1) {
[0]=>
string(86) "CMSSESSID00f4b28b="6ch03433vc17dsah4khr28oie2";Path="/";Domain="www.bfer.ro";Port="80""
}
["connectorVersionGuid"]=>
string(36) "0ccdac3e-0887-4f73-9864-9b9ee3c0c66e"
["connectorGuid"]=>
string(36) "7d00ba0e-947c-403f-b33b-886a7ee2a300"
["pageUrl"]=>
string(35) "http://www.bfer.ro/ro/curs-valutar/"
["outputProperties"]=>
array(3) {
[0]=>
object(stdClass)#7 (2) {
["name"]=>
string(6) "moneda"
["type"]=>
string(6) "STRING"
}
[1]=>
object(stdClass)#8 (2) {
["name"]=>
string(9) "cumparare"
["type"]=>
string(6) "STRING"
}
[2]=>
object(stdClass)#9 (2) {
["name"]=>
string(7) "vanzare"
["type"]=>
string(6) "STRING"
}
}
}

上面的 JSON 是由这个 PHP 脚本生成的:

<?php

$userGuid = "8f65f01f-c6bc-42a4-914d-879efd159abd";
$apiKey = "private";

// Issues a query request to import.io
function query($connectorGuid, $input, $userGuid, $apiKey) {

$url = "https://query.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"import-io-client: import.io PHP client",
"import-io-client-version: 2.0.0"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("input" => $input)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);

return json_decode($result);
}

// Query for tile Curs Banca Comerciala Feroviara
$result = query("7d00ba0e-947c-403f-b33b-886a7ee2a300", array(
"webpage/url" => "http://www.bfer.ro/ro/curs-valutar/",
), $userGuid, $apiKey, false);
var_dump($result);

?>

有没有办法使用第二个 PHP 脚本从这个 JSON 中仅输出字符串 (6) - 例如 string(6) "4.3550"。我该怎么做?

最佳答案

您需要编写此代码以仅输出您要查找的字符串 (6):

<?php

$userGuid = "8f65f01f-c6bc-42a4-914d-879efd159abd";
$apiKey = "private";

// Issues a query request to import.io
function query($connectorGuid, $input, $userGuid, $apiKey) {

$url = "https://query.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"import-io-client: import.io PHP client",
"import-io-client-version: 2.0.0"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("input" => $input)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);

return json_decode($result);
}

// Query for tile Curs Banca Comerciala Feroviara
$result = query("7d00ba0e-947c-403f-b33b-886a7ee2a300", array(
"webpage/url" => "http://www.bfer.ro/ro/curs-valutar/",
), $userGuid, $apiKey, false);

if(count($result->results) > 0) {
foreach ($result->results as $index => $row) {
echo($row->html_1);
echo '<br />';
}
} else {
echo 'No results found';
}

?>

我刚刚添加了 if(count($response->results) > 0) 以防 API 不返回任何内容(如果请求的页面不可用,可能会发生这种情况)。

我希望它能回答您的问题。如果您需要任何帮助,请告诉我:)

关于json - import.io JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29566648/

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