3

我需要从 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 中只输出 string(6) - 例如string(6) "4.3550"。我怎么做?

4

1 回答 1

4

您需要编写此代码以仅输出您要查找的字符串 (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 不返回任何内容(如果请求的页面不可用,可能会发生这种情况)。

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

于 2015-04-11T07:51:08.380 回答