I am trying to access the json data from an API, and displaying it on the webpage for the time being. I am able to fetch the data array, but I want only 1 field in that data. Below is my code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'ACCEPT: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,"https://www.localmed.com/providers/us_0651651651_9vg5u1r2g1tw3e5_f24c866/openings/?reasonForVisitId=us_1223_new_teethcleaning-adult");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
//$json_a=json_decode($result,true);
var_dump(json_decode($result, true));
var_dump ($result[1]['time']);
Below are the results,
I need to get only the 'time' field. please help. I am getting error undefined index 'time'
array (size=1)
'openings' =>
array (size=10)
0 =>
array (size=4)
'id' => string 'us_0651651651_9vg5u1r2g1tw3e5_f24c866_20160909T130000Z_67cbe00' (length=62)
'uri' => string '/providers/us_0651651651_9vg5u1r2g1tw3e5_f24c866/openings/us_0651651651_9vg5u1r2g1tw3e5_f24c866_20160909T130000Z_67cbe00/' (length=121)
'display' => null
'time' => string '2016-09-09T13:00:00Z' (length=20)
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 am trying to send file via curl, but keep getting this error:
PHP Notice: Array to string conversion in /home/sasha/Documents/OffProjects/test/index.php on line 26
This is my code at the moment:
$target_url = 'https://address';
$file_name_with_full_path = realpath('mpthreetest.mp3');
$post = [
'textNote' => 'This is test',
'audioFile' => '#'.$file_name_with_full_path,
'department' => 'Test',
'timeDetected' => round(microtime(true) * 1000),
'subjectLine' => 'Test Test',
'recipients' => ['phone-number' => '111111111']
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
How can I make this working?
You can't have an array as a value, it's trying to convert the post data into a string. Check what the website you are posting to is expecting for "recipients", my guess would be some form of JSON string.
When you pass an array to CURLOPT_POSTFIELDS it will try to build a form-data content (see http://php.net/manual/en/function.curl-setopt.php for details).
But you have nested arrays here:
....
'recipients' => ['phone-number' => '111111111']
...
and this cannot work. You'll have to find a different way to pass multiple values as recipients, either as a JSON string as #fire mentioned, or with a serialized value, or whatever transformation you prefer.
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.
In the PayPal tutorial on making your first call it gives you a JSON string that I've found impossible to post. I've exhausted Google and I just can't seem to figure out how to post this data to PayPal. The code below gives me a 415 error code ("unsupported media type"). I've been stuck on the error for hours and am growing tired of it. I'll be grateful to anyone that can help me. I've tried to make my description clear in case we do find an answer others in the future will be able to find it easily.
$post_data = array (
'intent' => 'sale',
'redirect_urls' => array (
'return_url' => 'http://example.com/return',
'cancel_url' => 'http://example.com/cancel'
),
'payer' => array (
'payment_mehod' => 'paypal'
),
'transactions' => array (
'amount' => array (
'total' => '7.47',
'currency' => 'USD'
)
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $result_array['access_token']]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
$result = curl_exec($ch);
Edit: In the second to last line of provided code I have also used http_build_query and I've also tried just simply sending it as a multidimensional array. The error code 415 returns for both of these.
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Authorization: Bearer ' . $result_array['access_token']
)
);
$URL:https://demo.firstach.com/https/TransRequest.asp?Login_ID=someit&Transaction_Key=somekey&Customer_ID=23&Customer_Name=Muhammad Naeem&Customer_Address=Address&Customer_City=city&Customer_State=HI&Customer_Zip=54000&Customer_Phone=--&Customer_Bank_ID=111111118&Customer_Bank_Account=12345678901234567890&Account_Type=Business Checking&Transaction_Type=Debit&Frequency=Once&Number_of_Payments=1&Effective_Date=12%2F05%2F2010&Amount_per_Transaction=10.00&Check_No=&Memo=&SECCType=WEB
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // times out after Ns
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch); // run the whole process
print_r($result);
curl_close($ch);
i also used file_get_conent and fopen but all are returning me BAD REQUEST error,
please help me out
for more detail please see the link below
http://www.uqwibble.com/Phase-2/ach.php
Well assumign the code you posted is accurate then this line is the issue:
$URL:https://demo.firstach.com/https/TransRequest.asp?Login_ID=someit&Transaction_Key=somekey&Customer_ID=23&Customer_Name=Muhammad Naeem&Customer_Address=Address&Customer_City=city&Customer_State=HI&Customer_Zip=54000&Customer_Phone=--&Customer_Bank_ID=111111118&Customer_Bank_Account=12345678901234567890&Account_Type=Business Checking&Transaction_Type=Debit&Frequency=Once&Number_of_Payments=1&Effective_Date=12%2F05%2F2010&Amount_per_Transaction=10.00&Check_No=&Memo=&SECCType=WEB
here it looks liek you attemtp to define $URL but when you use it with cURL you are referencing $url. The varibales are case sensitive. Secondly you have $URL: which is not valid you want to use $url =.
Addiitonally i would encode the params like this:
$baseurl = 'https://demo.firstach.com/https/TransRequest.asp';
$params = array(
'Login_ID' => 'someit',
'Transaction_Key' => 'somekey',
'Customer_ID'= => 23,
'Customer_Name' => 'Muhammad Naeem',
'Customer_Address' => 'Address',
'Customer_City' => 'city',
'Customer_State' => 'HI',
'Customer_Zip' => '54000',
'Customer_Phone' => '--',
'Customer_Bank_ID' => '111111118'
'Customer_Bank_Account' => '12345678901234567890'
'Account_Type' => 'Business Checking'
'Transaction_Type' => 'Debit'
'Frequency' => 'Once'
'Number_of_Payments' => 1,
'Effective_Date'=> '12/05/2010',
'Amount_per_Transaction' => '10.00',
'Check_No'=> '',
'Memo'=> '',
'SECCType' => 'WEB'
);
$url = sprintf('%s?%s', $baseurl, http_build_query($params));
That way http_build_query will take care of all your url encoding and you can work with an array before hand so its easy to see whats going on and add/remove/change paramters. Alternatively if its a post request you could jsut use:
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
which will take care of all the parameter encoding and what not directly from the array this way they dont need to be appended manually to the $url.