How to handle multiple request coming at the same time php - php

I have configured a url to other platform which is webhook for them. In this URL, order are coming simultaneously, which i manipulate and save it to the database.
The problem is that sometimes order timings are exactly same, so the orders data are mixed with each other and wrong information is saving into the database.
How would i handle this situation in php as i am using core php.
So that when two or more request are coming together, so each will be executed one by one.
For reading the input data sent to the url is
file_get_contents('php://input');
PHP CODE :
case "order_relay":
$body = file_get_contents('php://input');
$data = (array) json_decode($body);
$orderData = (array) $data['order']->details;
break;

Related

Trying to use a JSON API that isn't technically an API

I just started playing D&D, and I'm working on a project for my group to help us play the game a littler easier. We all created Characters on D&D Beyond. They don't have an official API, but apparently, if you type in "your-character-sheet/json" - you can get some JSON output about your character.
I attempted to grab this data in php as I normally would
<?php
$request = "https://www.dndbeyond.com/profile/PixiiBomb/characters/9150025/json";
$url = file_get_contents($request);
$json = json_decode($url, true);
echo $json["character"]["id"];
?>
This should echo out: "9150025" - but I actually see nothing. It's totally blank. So I tried to echo $url, and this is what I see.
Which of course, it not what I should be seeing (because that is formatting from their website, minus the stylesheets)
I went back to the JSON view and manually saved the data as pixii.json This time using the following:
$request = "uploads/dndbeyond_sheets/pixii.json"; // Saved to my harddrive
$url = file_get_contents($request);
$json = json_decode($url, true);
echo $json["character"]["id"];
THIS WORKS if I manually save the file. But that would mean that every time we level up, I would have to manually save all of our JSON data, and run some code to read it and update it.
Is there something else I can try to allow me to use this URL instead of having to manually save the page?
Ideally, I would like to have all of my code written , and then when we level up, I press a button and grab the new data from the site (which I don't need help with, that's not part of the question). Instead of manually visiting each website, saving the data, and then parsing it.

Store and prepare data in php with specific parameter request url

I have seen here this url json response data file from Highstock:
https://www.highcharts.com/samples/data/jsonp.php?filename=msft-c.json
So the data (encoded in json format) taken via GET request changes every time that the filename parameter value is different, for example in our case we can call different data by using these other 2 parameters: aapl-c.json and goog-c.json.
How can be performed in php the same data preparation for store multiple data resources with different filenames values request? And can we call a specific portion of data by using different parameter values? Also a very simple explanation of the logic behind this process would be enough.
I am skipping some verifications here, but the process is quite simple, this is a quite basic GET var analysis. When there is ?varname=value at the end of th URL, PHP gets the variable in the $_GET array. However, it is quite unsafe to use it raw, you should filter it before.
Another advice here is not to try to load a file dynamically from this var value or these kind of things, for security reasons. It is better to only allow desired values (used a switch here that have a quite strange syntax, but if/else fit just the same).
Then with php, you have to create your document with a display, like echo. In this case, it is JSON, so you just have to output your JSON.
Would be something like:
<?php
//sanitize the value from $_GET array
$getVar = filter_input(INPUT_GET, 'filename', FILTER_SANITIZE_STRING);
//do various things depending on value
switch($getVar){
case 'aapl-c.json':
$display = '{ "content": "i am JSON 1" }';
break;
case 'goog-c.json':
$display = '{ "content": "i am JSON 2" }';
break;
default:
$display = '';
}
//send the json data to the document
echo $display;
//terminate script for security
exit;

Database Post to another Database

I'm currently a front end developer for a website which lets users sign up and then make purchasing of items.For transactions, I'm using an API which the payment gateway company provided but when passing data via AJAX certain sensitive data are exposed for the users to see. I was wondering if there is a way that I can pass my parameters to a database (which I'm going to create), and then let the database do the AJAX posting so sensitive parameters can be hidden from the users. Also the database will store the response from the callback.
The ajax doesn't have to post to a database unless you want it to, but even then the database would not be what posts the data to a remote api. It's up to your backend php to do that.
Using jquery for the ajax call you would use something like:
$.getJSON("your_backend.php", function(result){
//whatever you want to do with the json returned from the remote site.
}
In your_backend.php:
<?php
$user = "user name";
$key = "key"
$headers = array(
'http'=>(
'method'=>'GET',
'header'=>'Content: type=application/json \r\n'.
'user:$user \r\n'.
'key:$key'
)
)
$context = stream_context_create($headers)
$url_returns = file_get_contents($api_url, false, $context);
echo $url_returns
?>
I haven't debugged this, so it won't work until you go through it, but it should give you an idea about how to proceed. Depending on how complex the connection is with the remote api you may need to use the cURL library for php instead of the file_get_contents.
If you want to save the information in your database you would write an insert statement from the backend php.

NetSuite - Accept webhook POST data to create record using ASP.NET or PHP

We have a lead generation form at Unbounce.com that is capturing lead data. They have a webhook that can transmit the data via POST to any URL that can accept it and process it. We would like to build a page that accepts this data and processes it in NetSuite (probably via the SuiteScript API's, but not sure). http://www.netsuite.com/portal/developers/resources/APIs/Dynamic%20HTML/SuiteScriptAPI/MS_SuiteScriptAPI_WebWorks.1.1.html
Variables To Get From POST
The following variables will be passed from the form in this order to the NetSuite processing page:
prog
first_name
last_name
email
parents_email
i_am_a_
phone_number
parents_phone_number
comment
Additional Page Variables To Attempt To Grab
Reading the example code below it looks like we can grab and store a few additional items. If so it would be good to store them in the CRM in the visitors profile for future reference:
page_id
page_url
variant
REQUEST FOR SAMPLE CODE
Since our preferred development enviornment is ASP.NET, can you provide sample code that can accept POST data from a webhook and create a new CRM record within our NetSuite account?
SAMPLE PHP CODE TO GET DATA FROM POST
Example code can be found at http://support.unbounce.com/entries/307685-how-does-the-form-webhook-work
If this were a PHP page you would grab the variables in the following fashion:
// This is a sample PHP script that demonstrates accepting a POST from the
// Unbounce form submission webhook, and then sending an email notification.
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// First, grab the form data. Some things to note:
// 1. PHP replaces the '.' in 'data.json' with an underscore.
// 2. Your fields names will appear in the JSON data in all lower-case,
// with underscores for spaces.
// 3. We need to handle the case where PHP's 'magic_quotes_gpc' option
// is enabled and automatically escapes quotation marks.
if (get_magic_quotes_gpc()) {
$unescaped_post_data = stripslashes_deep($_POST);
} else {
$unescaped_post_data = $_POST;
}
$form_data = json_decode($unescaped_post_data['data_json']);
// If your form data has an 'Email Address' field, here's how you extract it:
$email_address = $form_data->email_address[0];
// Grab the remaining page data...
$page_id = $_POST['page_id'];
$page_url = $_POST['page_url'];
$variant = $_POST['variant'];
However I don't know the code to use to get it into NetSuite. After reviewing the SuiteScript API from NetSuite it looks like we should be using nlobjRequest or nlapiRequestURL, but I have zero knowledge on how to integrate this with the sample PHP page above.
Thanks for all your help for a NetSuite noob.
You would need to create a RESTlet in NetSuite. You can find the documentation on NetSuite's Help Guide. RESTlets are part of NetSuite's SuiteScript API. They are written as JavaScript (of course, using the APIs provided by NetSuite).
When you create a RESTlet, you will be given a URL. Which you should use for your Unbounce web hook. Your RESTlet should parse the JSON data being passed by Unbounce.com.

How to construct multiple id values to a curl request

To explain more. I have a current data-store which will feed a .json file from a remote server. I can grab this remote file through an id added to a curl function.
ie. $json_url = 'example.com/datastore.json?id='. $result['id'].'';
This example is housed within a foreach loop pulling the id's from a database. This json_url example works for requesting 1 id at a time. I have instituted on the remote server the ability to request multiple ids at once through comma separated values.
ie. $json_url = 'example.com/datastore.json?id=1,id=2,id=3
Now if I am building the json_url on the fly for multiple requests, how should I do this?
I want this type of a $json_url result:
`$json_url = 'example.com/datastore.json?id=1,id=2,id=3`
BUT those id values are id values that have been returned from the foreach loop, and with each id value comes an added json file and an added id value to the json_url. In this case instead of 1, it is 3 .json files I will have retrieved with 1 request to the server, the number here is ambiguous as it is ultimately dependent on how many id values are found on the query.
If I am unclear with my examples, let me know and I will take the time to make it more clear.
`
Why not using an array?:
$json_url = 'example.com/datastore.json?id[]=1&id[]=2&id[]=3
then you can loop using
foreach($_GET['id'] as $key => $val){}
//$_GET['id'] OR $_POST['id']...

Categories