I'm trying to use this api (currency exchange api). This returns something like this:
{
"from":"USD",
"to":"HKD",
"rate":7.7950999999999996958877090946771204471588134765625
}
I send the request with file_get_contents() and this seems to work.
My problem is that I can't access the array key rate.
$r = file_get_contents('https://cc.hirak.cc/usd_hkd');
print_r($r['rate']); // nothing is shown. This is the problem.
print_r($r); // result shows ( all the array )
How can I access just the rate key?
you are accessing an string instead of array,
Try this code, I think this will work:
$r = json_encode(file_get_contents('https://cc.hirak.cc/usd_hkd'),true);
I made a form where people posts fname, lname, products and links. I did validation for names and product and links. Now the thing if someone posting adult or fraud or scam links then i also want to filter that out. I spent hours on this and cant find anything on internet. Now i decide to do this and write a some code on my own. I studied some thing in php manual and there i found to filter the array thats array_filter. On php.net With the reference of "rolf" notes there I found how to delete specific value from an array.
Now the thing is when i integrate this with my server side code the filter array also show with the users array and when i type the filter array the string is going to delete and array is empty. I am not very good with explanation so i also post a pic for better understanding whats going on.
What i want is filter array value not come until it has been called if the filter value array called then its going to delete the specific value from array. OR You guys have better solution
If the array is matched then say the user Good bye or Thanks your link posted now.
php
[if (isset($_POST['name'])) {
$name = $_POST['name'];
if (empty($_POST['name'])) {
echo "What are you doing MAN !! Try Again..";
} else {
echo "Thanks! You do a great JOB!";
}
$array = array ("bad.com");
$filtered_array = array_filter($array, function ($element) use ($name) { return ($element != $name); } );
print_r($filtered_array);
}
I am trying to receive JSON data from an external system, and then process and save to my local DB. When an event triggers, the external system sends the data below to my system.
The external system logs show:
Log 1: Date/Time: 16 August 2017 11:54:44 AM
Status: Failure
Server: Apache https://blahblah.com/abc/data.php
Status Code: OK
Event Content: [{"field":"_USERNAME","value":""},
{"field":"_PASSWORD","value":""},
{"field":"_TOKEN","value":""},
{"field":"_CODE","value":"L77H4XD6ZA"},
{"field":"_SUBMITTEDDATE","value":"2017.08.16.01.54"},
{"field":"_SUBMITTEDDATEEXT","value":"2017.08.16.01.54.39.610"},
{"field":"_EDITEDDATE","value":"2017.08.16.01.55"},
{"field":"_SEQUENTIALID","value":"39a1cad9-2582-e711-9477-06c7814985cc"},
{"field":"_COMPLETETYPE","value":"Complete"},
{"field":"_LANGUAGE","value":"en"},
{"field":"_TOTALTIME","value":"12.59"},{"field":"_LINKURL","value":"http%3a%2f%2fsurv.blah.com%2ftest3%3fusr%3dL77H4XD6ZA"},
{"field":"GENDER","value":"TEXT%3aFemale%3bVALUE%3a2"},
{"field":"AGE","value":"TEXT%3a40%2b-%2b44%3bVALUE%3a6"},
{"field":"STATE","value":"TEXT%3aVIC%3bVALUE%3a2"},
{"field":"END_CHC","value":"TEXT%3aComplete%3bVALUE%3a2"},
{"field":"D2H","value":""},
{"field":"D2V","value":""},
{"field":"PCODE","value":""},
{"field":"PSTATE","value":""},
{"field":"PREGION","value":""},
{"field":"STATEREGION","value":""},
{"field":"TEST1","value":""}]
So, in my php file (https://blahblah.com/abc/data.php) I have
$json = file_get_contents('php://input');
$obj = json_decode($json);
and I have been trying a foreach() loop to try and get each field and value, to then put into a database, like
foreach($obj as $key => $value)
{
$qry = $conn->prepare('INSERT INTO `key_value`(`db_id`, `rkey`, `rvalue`) VALUES (NULL,$key,$value)');
$qry->execute();
}
But it isn't working, and I either get errors about the foreach loop, or if I play around with just echoing or var_dumping, I get NULL in the response, when I use the Postman or ARC Chrome API testing Apps.
So, I suspect I am on the completely wrong track of how to do this.
Can anyone help guide me back?
EDIT: I have had a look at Receive JSON POST with PHP and a few others, but the working answers aren't clear.
You've got a couple of problems here. First, you need to pass true as the second parameter to json_decode in order to get an associative array instead of a stdClass. Then, when you're iterating the results, you don't need the $key, and the $value is each entry in the results array (each "row"). Try running this to see what's going on:
<?php
$json = <<<JSON
[{"field":"_USERNAME","value":""},
{"field":"_PASSWORD","value":""},
{"field":"_TOKEN","value":""},
{"field":"_CODE","value":"L77H4XD6ZA"},
{"field":"_SUBMITTEDDATE","value":"2017.08.16.01.54"},
{"field":"_SUBMITTEDDATEEXT","value":"2017.08.16.01.54.39.610"},
{"field":"_EDITEDDATE","value":"2017.08.16.01.55"},
{"field":"_SEQUENTIALID","value":"39a1cad9-2582-e711-9477-06c7814985cc"},
{"field":"_COMPLETETYPE","value":"Complete"},
{"field":"_LANGUAGE","value":"en"},
{"field":"_TOTALTIME","value":"12.59"},{"field":"_LINKURL","value":"http%3a%2f%2fsurv.blah.com%2ftest3%3fusr%3dL77H4XD6ZA"},
{"field":"GENDER","value":"TEXT%3aFemale%3bVALUE%3a2"},
{"field":"AGE","value":"TEXT%3a40%2b-%2b44%3bVALUE%3a6"},
{"field":"STATE","value":"TEXT%3aVIC%3bVALUE%3a2"},
{"field":"END_CHC","value":"TEXT%3aComplete%3bVALUE%3a2"},
{"field":"D2H","value":""},
{"field":"D2V","value":""},
{"field":"PCODE","value":""},
{"field":"PSTATE","value":""},
{"field":"PREGION","value":""},
{"field":"STATEREGION","value":""},
{"field":"TEST1","value":""}]
JSON;
$obj = json_decode($json, true);
foreach($obj as $currTuple)
{
echo $currTuple['field'].':'.urldecode($currTuple['value'])."\n";
}
Also some of the results are url encoded, so you'll probably want to decode that before persisting.
I am facing some logical issue. I am fetching data via upi status table which look like as follows.
on controller file i am creating the following logic..
$this->load->model('localisation/upc_status');
$statusArray = $this->model_localisation_upc_status->getUPCStatuses();
$this->data['statuses'] = $statusArray;
foreach ($statusArray as $sa){
$c[] = array($sa['status_id'] => $sa['name']);
}
echo $c['8'];
i am unable to get the name in-stock. instead i am getting Array in-place of it.
Try This,
$c[$sa['status_id']] = $sa['name'];
If you still can't get, adjust levels of the array.
Use print_r(array) to check arrays at required debugging points.
I'm new to PHP and having a problem reading checkboxes submitted by a form. Before explaining it i would like to mention that i'm trying to edit a much larger application and the data cannot be sent in any other way. It's just a matter of finding a good method of reading what is being sent.
When a normal text input is sent, the form will post the following:
custom[0][type]="text"
custom[0][name]="VariableName"
custom[0][value]="VariableName"
Basically there is a main "custom" multidimensional array that has several elements (0,1,2,3 etc) and each element has name and value.
However, when one of the elements is a checkbox, the following stuff gets posted:
custom[1][type]="list"
custom[1][name]="SelectedOptions"
custom[1][value]="Value1"
custom[1][value]="Value3"
custom[1][value]="Value5"
Getting to the PHP side of things, this is the code i'm using to read the data sent by the form. The code below works ok in scenario 1 (with text based inputs) but only reads one value when we have list type custom data.
foreach($_POST['custom'] as $item){
if($item['value'] != "") echo $item['name'].'='.$item['value']
}
The problem is that $item['value'] only reads one of the values, not all 3. How can i get all 3 values in a variable ? It probably is a very easy thing...
To put it all combined, this is what it is sent with POST (3 checkboxes are checked for Variable2)
custom[0][name] Variable1
custom[0][type] text
custom[0][value] ValueForVariable1
custom[1][name] Variable2
custom[1][type] checkbox
custom[1][value] Value1
custom[1][value] Value3
custom[1][value] Value5
And this is what print_r($_POST) shows for the posted data above
[custom] => Array
(
[0] => Array
(
[value] => ValueForVariable1
[name] => Variable1
[type] => text
)
[1] => Array
(
[value] => Value1
[name] => Variable2
[type] => checkbox
)
Just to be sure we're all on the same page, the actual data is generated by a more complex system and we can't really change that. I'm interested in seeing how we can read all 3 values for Variable2 that are sent in the POST.
Thanks !
EDIT:
With the extra information that you've since provided, I see that I originally misunderstood the problem.
Since you have no control over over the form that sends the data to your PHP script, and thus can't change it to ensure that the later checkboxes with the same name do not overwrite the earlier ones, you'll have to get access to and process the raw post data itself.
$postdata = file_get_contents("php://input");
echo $postdata;
... will output the postdata just like a GET querystring: blah=1&blah=2&blah=3 (blah indicates 3 form fields with the same name blah, the first two of which would be overwritten in $_POST leaving $_POST['blah'] = 3).
with a little exploding on & and looping and parsing looking for the variables in question, or even any conflicting variables, will get you where you're trying to go.
Original Answer:
HTML forms only submit checkboxes (or radio buttons) that have been checked. If they haven't been checked, the browser won't send the data back to the server.
The main way to solve this is to know what you're looking for on the back end and test for it (i.e. if (isset($_POST['checkboxname'])).
If you truly need a general purpose backend that will dynamically incorporate all checkbox elements, checked or not, the way I've solved this in the past is to use javascript to record all form elements on the page and submit that info with the rest of the form (and, in my case, also submit whether that field was changed or not).
Here's a function that you can run at the top of the script to treat POST the way you expect it to be treated from your ASP background:
function post_process() {
$rawpostdata = file_get_contents('php://input');
if (!$rawpostdata) return;
$fields = explode('&', $rawpostdata);
$post = array();
foreach ($fields as $field) {
list($key, $val) = explode('=', $field);
if (isset($post[$key])) $post[$key] .= ','.$val;
else $post[$key] = $val;
}
$_POST = $post;
}