Basically I need some help sending array information through redirects in PHP. I have the following code:
Page1: I created the query string and sent it to page 2, I can get the data in $_GET on page 2.
$qstr = http_build_query(
array(
'products_array' => $products,
'quantity' => $_POST['quantity']
)
);
header('Location: registration.php?' . $qstr);
Page 2/3 I get the query string from $_server and concatenate it with the redirect page
header('Location: login.php?' . $_SERVER['QUERY_STRING']);
I can send the data using normal href links, but if I try to send it like above I can't retrieve the data past page 2.
If you could give me some input on this problem it would really help.
Thanks.
Edit:
echoing $qtystr gives:
products_array%5B0%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F61Y-rF9tF8L.SL1100.jpg&products_array%5B0%5D%5Bbrand%5D=Charmander&products_array%5B0%5D%5Bprice%5D=25&products_array%5B1%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F61vgC3GDI2L.SL1100.jpg&products_array%5B1%5D%5Bbrand%5D=Squirtle&products_array%5B1%5D%5Bprice%5D=15&products_array%5B2%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F51TnHKT4oML.SY300.jpg&products_array%5B2%5D%5Bbrand%5D=Bulbasaur&products_array%5B2%5D%5Bprice%5D=10&products_array%5B3%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F41MpzoPshAL.SX300.jpg&products_array%5B3%5D%5Bbrand%5D=Chikorita&products_array%5B3%5D%5Bprice%5D=20&products_array%5B4%5D%5Bitem%5D=http%3A%2F%2Fecx.images-amazon.com%2Fimages%2FI%2F51BIJR%252BIqDL.SX355.jpg&products_array%5B4%5D%5Bbrand%5D=Mudkip&products_array%5B4%5D%5Bprice%5D=20&quantity%5B0%5D=0&quantity%5B1%5D=0&quantity%5B2%5D=0&quantity%5B3%5D=03&quantity%5B4%5D=0
which is a stupid amount of information to pass in a string (just testing stuff out), sorry, but it's all the information from the two arrays I needed.
Edit2:
I managed to fix it, I didn't give you guys enough info sorry. The problem was actually that I did not set the form action of my pages to preserve the data string, so it kept getting lost as I was processing the form. Thanks for all the advice, I'll be sure to try it out next time, especially sessions.
Using only PHP
To encode the data
$products = "some product";
$qtd = 1;
$array = array('products_array' => $products, 'quantity' => $qtd);
$json_str = json_encode($array);
echo $json_str;
header('Location: registration.php?' . $qstr);
To decode the data
$data = json_decode($post_data); //to decode the string into an object
echo $data->products_array . "\r\n";
echo $data->quantity . "\r\n";
Related
I have the URL https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680
which outputs statistics.
For example:
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
I want to get the value of link count.
Can anyone help?
I tried to do:
<?php
$content = file_get_contents("https://android.rediptv2.com/ch.php?usercode=5266113827&pid=1&mac=02:00:00:00:00:00&sn=&customer=GOOGLE&lang=eng&cs=amlogic&check=3177926680");
$result = json_decode($content);
print_r( $result->link );
?>
But it didn't work.
Put the JSON in an editor and you'll see that it's an array and not an object with the link attribute. This is why you cannot access it directly. You have to loop over the items and then you'll be able to access the link property of one of the items. If you need to access the link by id, as you asked 4 months later, then just create a dictionnary in an array indexed by id and containing just the interesting data you need.
PHP code:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
echo '$items = ' . var_export($items, true) . "\n\n";
// Create a dictionnary to store each link accessible by id.
$links_by_id = [];
// Loop over all items:
foreach ($items as $i => $item) {
// Show how to access the current link.
echo "Link $i = $item->link\n";
// Fill the dictionary.
$links_by_id[(int)$item->id] = $item->link;
}
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
// Example of access by id:
// The id seems to be a string. It could probably be "1895" or "zhb34" or whatever.
// (If they are only numbers, we could convert the string to an integer).
$id = "1859";
echo "\nAccess with id $id = " . $links_by_id[$id] . "\n";
Test it here: https://onlinephp.io/c/e8ab9
Another important point: You are getting a 403 Forbidden error on the URL you provided. So typically, you will not obtain the JSON you wanted.
As I explained in the comment below, I think that you will not be able to access this page without having a fresh URL with valid query parameters and/or cookies. I imagine you obtained this URL from somewhere and it is no longer valid. This is why you'll probably need to use cURL to visit the website with a session to obtain the fresh URL to the JSON API. Use Google to find some examples of PHP scraping/crawling with session handling. You'll see that depending on the website it can get rather tricky, especially if some JavaScript comes into the game.
I am setting up a Paypal sandbox. Everything works fine. When the transaction is done, paypal POSTS some data to my "notify_url" page (which I've named process-payment.php).
Now, when I post:
$array = $_POST;
$encodedString = json_decode($array);
Now, I can PUT that encoded string in the database, and it looks like:
{"mc_gross":"10.00","protection_eligibility":"Eligible",
"address_status":"confirmed","payer_id"}
Now, my big question is, how can I get THAT (^^^) into an associative array, where I can store those values in a database that records the transaction? Thank you so much for your help in advance! I've already tried:
$pp_array = file_get_contents('php://input');
$arrayDump = json_encode($pp_array);
$pp_array = json_decode($pp_array, true);
Which, obviously, didn't work. So, kinda hoping someone can give me a little tutelage here!
You are trying to use json_decode on an bad array because your payer_id is NULL. Consider filling or removing it. This is a working example with a filled payer_id:
$json = '{"mc_gross":"10.00","protection_eligibility":"Eligible", "address_status":"confirmed","payer_id":"2"}';
$json_asoc = (json_decode($json, true));
print $json_asoc['mc_gross']; // 10
For details see here
So, turns out that the code I used to make it work was this:
$array = $_POST;
$arrayDump = json_encode($array);
file_put_contents('payment-record.txt', $arrayDump);
$fileContents = file_get_contents('payment-record.txt');
$pp_array = json_decode($fileContents, true);
That gave me a workable array. Not sure why I had to write it first, but there you go.
I am wanting to use a form confirmation to display a single use code and then discard the code so it wont be used again. So far this is what I have:
$codes = array(
'810',
'0190',
'1924',
'481',
'2941',
'8777',
'092',
'432',
'984',
'172',
'8483'
);
$rand_code = array_rand($codes);
$code_gen = $codes[$rand_code];
return $confirmation = 'Here is your code:' . $code_gen;
This shows me a random code each time I submit the form so it works perfect. I need to actually store that code and not use it again. What would be my best solution? Any help would be greatly appreciated.
Shuffle to get a random one, then pop it out, bam.
shuffle($codes);
while($code_gen = array_pop($codes)){
return $confirmation = 'Here is your code:' . $code_gen;
}
If you prepend the datetime to the ticketnumber then you are guaranteed that your number is not used again without needing to store it. (unless we have time travellers).
e.g. 110912032634 = ticket 1 and 110912032785 = ticket 2
If you mean by "later on" in the session, use sessions, etc...
If you mean by "later on" : after a month to collect data, then store it in a database
I have a simple list of orders which a user can filter by status (open,dispatched,closed). The filter dropdown triggers a post to the server and sends the filter value through. Orders are listed out 10 to a page with pagination links for any results greater than 10. Problem is when I click the pagination links to view the next page of results the filter value in the post is lost.
public function filter_orders() {
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$filter = $this->input->post('order_status_filter');
$config = array();
$config["base_url"] = base_url() . "control/orders/filter_orders";
$config["per_page"] = 10;
$config['next_link'] = 'Next';
$config["uri_segment"] = 4;
$config['total_rows'] = $this->model_order->get_all_orders_count($this->input->post('order_status_filter'));
}
How can I make the pagination and filter work together. I've thought about injecting a query string in to the pagination links but it doesn't seem like a great solution.
The answer is very simple, use $_GET. You can also use URI segments.
i.e, index.php/cars/list/5/name-asc/price-desc'
The main reason you'll want to use $_GET is so you can link other users so they see the same result set you see. I'm sure users of your web app will want this functionality if you can imagine them linking stuff to each other.
That said, it would be ok to ALSO store the filters in the session so that if the user navigates away from the result set and then goes back, everything isn't reset.
Your best bet is to start a session and store the POST data in the session. In places in your code where you check to see if the user has sent POST data, you can check for session data (if POST is empty).
In other words, check for POST data (as you already do). If you got POST data, store it in the session. If a page has no POST data, check to see if you have session data. If you do, proceed as if it was POSTed. If you have both, overwrite the session with POST. You'll want to use new data your user sent you to overwrite older data they previously sent.
You either put everything in $_GET or if the data is sensible, put it in $_SESSION. Then it travels between pages.
In your case there seem to be no reason to put your filter data anywhere else than in $_GET.
A query string does seem the best solution. You could store it in the session or in cookies as well, but it makes sense to also store it in the query string.
Store it in cookies or the session if you want to remember the user's choice. Which seems like a friendly solution. It allows the user to keep their settings for a next visit, or for another page.
Store it in the query string, because going to 'page 2' doesn't tell you anything if you don't know about filters, page size or sorting. So if a user wants to bookmark page 2 or send it by e-mail, let them be able to send a complete link that contains this meta information.
Long story short: Store it in both.
maybe its not a right answer but, give it a try
<?php
// example url
$url = "index.php?page=6&filter1=value1&filter2=value2";
// to get the current url
//$url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
// change the page to 3 without changing any other values
echo url_change_index( $url, "page", 3 );
// will output "index.php?page=3&filter1=value1&filter2=value2"
// remove page index from url
echo url_change_index( $url, "page" );
// will output "index.php?filter1=value1&filter2=value2"
// the function
function url_change_index( $url, $name = null, $value = null ) {
$query = parse_url( $url, PHP_URL_QUERY );
$filter = str_replace( $query, "", $url );
parse_str( $query, $parsed );
$parsed = ( !isset( $parsed ) || !is_array( $parsed ) ) ? array() : $parsed;
if ( empty( $value ) ) {
unset( $parsed[$name] );
}
else {
$parsed[$name] = $value;
}
return $filter.http_build_query( $parsed );
}
?>
I'm a newbie with drupal.
I was trying to send data from drupal to node.js function and save the data from node into mongo.
I wrote a function in php
function sample_working_submit($form, &$form_state) { //Function to connect with node and save into Mongo using node
$Name = check_plain($form_state['values']['Name']);
$Age = check_plain($form_state['values']['Age']);
$request_url = "http://10.20.5.112:3001/save/$Name/$Age ";
$response = drupal_http_request($request_url);
}
This is working as long as there is no 'space' between the names(input). How can save the input with spaces.Why does this issue came?
How can i rewrite the function as post?
<?php
$url = http://localhost:8080/myservlet;
$data = array (
'name' => check_plain($form_state['values']['Name']),
'age' => check_plain($form_state['values']['Age'])
);
$response = drupal_http_request($url, $headers, 'POST', json_encode($data));
?>
This would POST data to URL. Note that you need to change logic in server side as well to receive POST data instead of GET data.
Refer here for more info
If the space between the names is the issue, try using urlencode().
The code will be something like:
$Name = urlencode(check_plain($form_state['values']['Name']));
For POST requests I use SimpleBrowser instead of drupal_http_request(). It's easy to use and you'll be able to pass strings in any form.