How can I dump the results into a MySql db? Specially decode gzip and parse it to an HP array then dump into a db.
<?php
$connection = mysqli_connect("localhost","root","Aruns@cal");
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
$db_select = mysqli_select_db($connection, "arunslocal");
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
$params = http_build_query(array(
"api_key" => "xxxxxxx",
"format" => "JSON"
));
$result = file_get_contents(
'https://www.parsehub.com/api/v2/projects/tmnx0bN0Ty0U/last_ready_run/data?'.$params,
false,
stream_context_create(array(
'http' => array(
'method' => 'GET'
)
))
);
echo "1";
$arr = json_decode($result, true);
echo "2";
foreach((array)$arr as $item){
echo "3";
$name = $item['name'];
$value = $item['value'];
echo "4";
$sql = "INSERT INTO `arunslocal`.`scrap_yielddata`( `yield_name`, `yield_value`) VALUES(1,3)";
//$sql = "INSERT INTO `arunslocal`.`scrap_yielddata`( `yield_name`, `yield_value`) VALUES('$name','$value')";
if(!mysqli_query($connection,$sql))
{
die('Error : ' . mysqli_error($connection));
}
}
echo "5";
?>
How can I dump the results into a MySql db? Specially decode gzip and parse it to an HP array then dump into a db.
The data is gzip encoded
Your problem is the response data is gzip encoded.
As per parsehub's API documentation,
Solution
Try decoding the gzip response.
You can use
gzdecodeto decode the gzip response.Also fetch the API with format json instead.
You can then
json_decodeto convert it into an array and use it.Example
Output