Search code examples
jsonimport.io

import.io JSON data


I need to output some values from a JSON generated by import.io

This is the 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"
    }
  }
}

The above JSON was generated by this PHP script:

<?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);

?>

Is there a way to output only string(6) from this JSON using the second PHP script - for example string(6) "4.3550". How do I do that?


Solution

  • You need to write this code to output only the string(6) that you're looking for:

    <?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';
    }
    
    ?>
    

    I just added the if(count($response->results) > 0) in case the API is returning nothing (it may happens if the page requested is unavailable).

    I hope it's answering your question. Let me know if you need any help :)