How to decode response of eth_getlogs with PHP? - php

I succeeded in getting the contract log from infura with eth_getlogs.
But I don't know how to decode the retrieved data with PHP.
Please tell me how to decode retrieved data with PHP.
PHP code
$my_server_url = "https://mainnet.infura.io/v3/APIKey";
$ch = curl_init($my_server_url);
$data = array(
'jsonrpc' => '2.0',
'id' => 1,
'method' => 'eth_getLogs',
'params' => array(array(
"fromBlock" => "0x949986",
"toBlock" => "latest",
"topics" => ["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094"],
"address" => "0xf0A0293D762aF2AC36E57613D42aC36773eEAf51"
))
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
Retrieved Data:
{"jsonrpc":"2.0","id":1,"result":[{"address":"0xf0a0293d762af2ac36e57613d42ac36773eeaf51","blockHash":"0x348b7e3dcf6c76c483f872851650b7aed8f2f6d6042207b8f55e13a6c52a19cd","blockNumber":"0x949986","data":"0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000973616d706c653132330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e6372656174652073756363657373000000000000000000000000000000000000","logIndex":"0x85","removed":false,"topics":["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094","0x0000000000000000000000008790dc53157f7090b85785fc107360a23ad63a13"],"transactionHash":"0x0b9544631e221ed9569aae363d272a54dc853cf7f84610bda3c828c6f181784e","transactionIndex":"0x4a"},{"address":"0xf0a0293d762af2ac36e57613d42ac36773eeaf51","blockHash":"0xc287eb38486a137e33a85f641efdcc1f57b90d3f7889709503e53fec54657ff0","blockNumber":"0x9499f2","data":"0x00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000973616d706c65313233000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137365742061646472657373207375636365737300000000000000000000000000","logIndex":"0x60","removed":false,"topics":["0x70d79747edd06ece6bf0a5ee4429d0138fa4ccbeefaea4d21e0e511e9d81b094","0x0000000000000000000000008790dc53157f7090b85785fc107360a23ad63a13"],"transactionHash":"0xae01b5c746ce2229c566682208b7af1f0520e094dfc2adef69f22eb8dd6f3bf2","transactionIndex":"0x4a"}]}
I want to decode data section of retrieved data.

Related

cURL Redirect Returned URL

I want to send data from site A to site B. I've successfully transferred data but I want to redirect the url to the returned url generated from Curl. Below is my code.
$jsonData = array(
'first_name' => "$fname",
'last_name' => "$lname",
'phone_number' => "$phone",
'gender' => "$gender",
'email' => "$email",
'businessname' => "$businessname",
'natureofbusiness' => "$biznature",
'address' => "$address",
'utm' => "esg",
'date' => "$d"
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
This is the result
{"data":{"success":true,"redirectUrl":"https://url.com/authorize
/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGlt
YSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-
U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}
I need help to redirect to that Url.
Convert JSON result into array
$result = '{"data":{"success":true,"redirectUrl":"https://url.com/authorize/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGltYSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}';
$result = json_decode($result, true);
Now get url from array.
if(isset($result["data"])){
if($result["data"]["success"]==true){
$url = $result["data"]["redirectUrl"];
header("location:".$url);
}
}

JSON API returning error although parameters are being sent

I'm sending a request to a JSON API system (http://help.solarwinds.com/backup/documentation/Content/service-management/json-api/login.htm) using PHP:
$base = 'https://cloudbackup.management/jsonapi';
$vars = array(
"jsonrpc" => "2.0",
"method" => "Login",
"params" => array(
"partner" => "partner",
"username" => "username",
"password" => "pass",
),
"id" => "1",
);
$ch = curl_init( $base );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response, true);
But its returning this array in $output
Array
(
[error] => Array
(
[code] => -32700
[data] => 119
[message] => Parse error: Failed to parse request body: * Line 1, Column 1
'--------------------------' is not a number.
)
[id] => jsonrpc
[jsonrpc] => 2.0
)
I cannot work out why it's returning an error, because I'm sending the correct parameters that it says in the docs.
Can someone point me in the right direction or if I have missed something?
Set the content-type to application/json as curl is likely defaulting to sending it as x-www-form-urlencoded
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
also JSON-encode your array:
$jsonDataEncoded = json_encode($vars);
Full refactored sample:
$base = 'https://cloudbackup.management/jsonapi';
$vars = array(
"jsonrpc" => "2.0",
"method" => "Login",
"params" => array(
"partner" => "partner",
"username" => "username",
"password" => "pass",
),
"id" => "1",
);
$jsonDataEncoded = json_encode($jsonData);
$ch = curl_init( $base );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response, true);

send multiple json data into api

I want to send these three array data into the external api. For that firstly I encode this into JSON, but always it sends the last array.
$params = array( array('receiverName' => 'sample_name',
'receiverEmail' => 'krishanuniyalas#gmail.com',
'senderEmail' => 'info#hotelpalacio.net',
'roomNumber' => '12456',
'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'),array('receiverName' => 'sample_name',
'receiverEmail' => 'karamuniyalas#gmail.com',
'senderEmail' => 'info#hotelpalacio.net',
'roomNumber' => '12456',
'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'),array('receiverName' => 'sample_name',
'receiverEmail' => 'krishanuniyalas#gmail.com',
'senderEmail' => 'info#hotelpalacio.net',
'roomNumber' => '12456',
'accessToken' =>'ca5629d0-6810-11e8-9d40-d7194ac0ac8d'));
$data_string = json_encode($params);
foreach($params as $pa) {
$r=json_encode($pa);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $r);
Here 's a working example ...
$data = [
[
'a' => 'bla',
],
[
'b' => 'blubb',
],
];
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "http://www.example.com/");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$result = [];
foreach ($data as $value) {
$json = json_encode($value);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
]);
$result[] = curl_exec($handle);
}
Open a valid curl resource and set the corresponding curl options for your purpose. You want to send a json string as post request. Furthermore you should tell the other side, that you send json data. Say curl, that you 're awaiting a response and the fire every time running through the foreach loop. That 's all ...
If you want to send the whole data at once leave out the foreach loop end encode the whole data array and send it.
You need to just create a simple query params like $data = http_build_query($params)
And then pass data to your curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

JSON Error on posting Data using PHP

Following is the coding I have done. But after posting the data, I am getting error as:
'message' => string ''from' and 'to' date must be given' (length=34).
Following is my code:
$auth_token=$_REQUEST['auth_token'];
$ch = curl_init('https://api.datonis.io/api/v3/datonis_query/thing_aggregated_data');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'X-Auth-Token:'.$auth_token,
'thing_key:6f2159f998',
'idle_time_required:true',
'from:2016/02/02 17:05:00',
'to:2016/08/29 17:10:00',
'time_zone:Asia/Calcutta',
'Content-Type:application/json',
),
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
//var_dump($response);
$responseData = json_decode($response, TRUE);
// Print the date from the response
var_dump($responseData);
Actually I also want to get the data but the details are contained in the post request data.
use "CURLOPT_POSTFIELDS":
$data = array("from" => "2016/02/02 17:05:00", "to" => "2016/08/29 17:10:00");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json')

Why is this PHP script the JSON results when I haven't told it to?

So I'm using the only source I've found for sending a post request to Google QPX API. I want to save it in a json_decoded PHP array, but for some reason the $result = curl_exec($ch); line doesn't work, and the json prints onscreen anyways.
Is there something I'm not understanding that is happening in the cURL? Thanks!
$data = array ( "request" => array(
"passengers" => array(
adultCount => 1
),
"slice" => array(
array(
origin => "BOS",
destination => "LAX",
date => "2015-09-09"),
array(
origin => "LAX",
destination => "BOS",
date => "2015-09-10"),
),
solutions => "10"
),
);
$data_string = json_encode($data);
$ch = curl_init('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MY-API-KEY');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
This:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
http://php.net/curl_setopt
Set this option to true if you want to save the result in a variable.

Categories