when trying to json_encode() an array, array not encoding
my array is :
$jsonData = array(
'addressId' => $addid,
'paymentMethod' => 2,
'transactionId' => $orderid
);
print_r($jsonData);
it prints :
Array ( [addressId] => 28 [paymentMethod] => 2 [transactionId] => ORDS63375431 )
and json encoding:
$jsonDataEncoded = json_encode($jsonData);
echo $jsonDataEncoded;
it prints :
{"addressId":"
what i am doing wrong?
here is my full code:
$addid = '<script>document.write($.session.get("addid"));</script>';
$orderid = $_POST["ORDERID"];
$accessToken = $_COOKIE['accessToken'];
$url = 'http://bookwise.co.in/app/api/carts/checkout';
$ch = curl_init($url);
$jsonData = array(
'addressId' => $addid,
'paymentMethod' => 2,
'transactionId' => $orderid
);
print_r($jsonData);
$jsonDataEncoded = json_encode($jsonData);
echo $jsonDataEncoded;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-API-KEY: '.$accessToken.''));
$result = curl_exec($ch);
Related
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.
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);
}
}
I was getting a response from a REST flight api in JSON format which i was not able to convert into an array from JSON.
I have tried json encoding but it was only showing printing the json response but not converting it into array
Php Controller:
public function search_flites() {php controller
header('Content-type: application/json');
$this->load->library('curl');
$result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
$Data = json_decode($result);
$session_id = $Data->SessionId;
$url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
$ch = curl_init($url);
$jsonData = array(
"user_id" => "Ettafly_APITest2019",
"user_password" => "Ettafly_TestPswd2019",
"access" => "Test",
"ip_address" => "13.235.39.41",
"session_id" => "$session_id",
"journey_type" => "OneWay",
"airport_from_code" => "DEL",
"airport_to_code" => "BOM",
"departure_date" => "2019-11-16",
"return_date" => "2019-11-18",
"adult_flight" => "1",
"child_flight" => "0",
"infant_flight" => "0",
"class_type" => "Economy",
"target" => "Test"
);
$ch = curl_init($url);
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$result2 = curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$Data2 = json_decode($result2, true);
}
You didn't send that request.
Check this out: https://www.php.net/manual/en/curl.examples.php#88055
<?php
public function search_flites() {
header('Content-type: application/json');
$this->load->library('curl');
$result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
$Data = json_decode($result);
$session_id = $Data->SessionId;
$url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
$ch = curl_init($url);
$jsonData = array(
"user_id" => "Ettafly_APITest2019",
"user_password" => "Ettafly_TestPswd2019",
"access" => "Test",
"ip_address" => "13.235.39.41",
"session_id" => "$session_id",
"journey_type" => "OneWay",
"airport_from_code" => "DEL",
"airport_to_code" => "BOM",
"departure_date" => "2019-11-16",
"return_date" => "2019-11-18",
"adult_flight" => "1",
"child_flight" => "0",
"infant_flight" => "0",
"class_type" => "Economy",
"target" => "Test"
);
$ch = curl_init($url);
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// You have to exec the curl request to get a response.
$result2 = curl_exec($ch);
$Data2 = json_decode($result2, true);
var_dump($Data2, JSON_PRETTY_PRINT);
}
by using below code
(
$payload = json_encode($jsonData);
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);
$jsonData2 = json_decode($result, true);)
instead of
(
$jsonDataEncoded = json_encode($jsonData, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
$curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// You have to exec the curl request to get a response.
$result2 = curl_exec($ch);
$Data2 = json_decode($result2, true);
var_dump($Data2, JSON_PRETTY_PRINT);)
Now i was able to conver the result into a array
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);
I am using a online laravel api doc to execute a program. When i run my code in postman, i get the error "pass number field as an array". But the number field is being already passed as an array field. What could i be missing below in my code ? Thanks in advance
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234,44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
When i print out my results, this is what i see
Array
(
[number] => Array
(
[0] => '44234200234,44234242002'
)
[from] => world
[content] => i love to code
)
{"status":"error","message":"Pass number field as an array"}
Here is the update of how it appears now
Array
(
[recipient] => Array
(
[0] => 44234200234
[1] => 44234242002
)
[from] => world
[content] => i love to code
)
You should try this:
public function testAPI(Request $request)
{
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
$ch = curl_init();
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($variables));
$result = curl_exec($ch);
$result = json_decode($result, TRUE);
return $data;
curl_close($ch);
}
You should try passing numbers on different indexes of array:
$variables = [
'number' => ['44234200234','44234242002'],
'from' => 'world',
'content' => 'I love to code',
];
Currently you are passing both numbers separated with , on 0 index.
Try Like This :
$numbersArray = array("44234200234","44234242002");
$variables = [
//'number' => ['44234200234','44234242002'],
'number' => $numbersArray,
'from' => 'world',
'content' => 'I love to code',
];
Ideally Old solution should work but you can try like this and check it.
You are passing your array as 1 string. To achieve with this (incorrect) approach, you would need to seperate the strings by a comma, and then convert each string to an integer.
'number' => ['44234200234,44234242002,1,2,3,4'], // this is ONE string
'number' => ['1', '2', '3', '4'] // this is many strings
What you want to do is pass an array of integers
numbers => [1, 2, 3]
Happy coding :-)
Try to use serialize function:
$on_call_back = 'https://learntoday.co.uk/var';
$id = '*****';
$url = $on_call_back.'?key='.$id;
$variables = [
'number' => serialize(['44234200234','44234242002']),
'from' => 'world',
'content' => 'I love to code',
];