Go to a URL in a loop [several times] - php

I've got a new SMS service for my php app. To send a message you need to go to an URL with the number and message as parameters.
This sms service sucks, is from an external company I know :(
//example.com/sms/?c=number&m=message
With an array of data I made a foreach loop to send each message.
<?php
// Users array
$users[] = ['Jhon', 123456789]
$users[] = ['Peter', 223456789]
$users[] = ['Sam', 323456789]
$users[] = ['Carl', 423456789]
// The loop
foreach ($users as $thisUser) {
// URL builded with the users data
$thisNumber = $thisUser[1];
$thisMessage = 'Hey,+your+name+is+' . $thisUser[0]
$request = sprintf('http://example.com/sms/?c=%d&m=Hey+%s+', $thisNumber, $thisMessage);
}
?>
Now, ¿witch is the best way to go that URL in a loop [several times]? Header redirection doesn't work for me. Maybe cURL...

I don't think JavaScript is a good solution. From the sound of it, you want to call that remote url so you can send an SMS. You can either use CURL or file_get_contents and just ignore the results (unless you need the results).

Related

How get data from url to array?

I use php pretty badly. I'm not a programmer just doing something for my private things.
I have such a problem I would like to download data from my PV production. The data is in the form as below. How to download data from a url in php, respectively, to group them and send them to the array ??
Thank you in advance for all your help.
{"sid":62923,"dataunit":"kWh","data":[{"time":"2019-08-01","no":"1","value":"27.7"},{"time":"2019-08-02","no":"2","value":"24.0"},{"time":"2019-08-03","no":"3","value":"19.9"},{"time":"2019-08-04","no":"4","value":"25.3"},{"time":"2019-08-05","no":"5","value":"0.0"},{"time":"2019-08-06","no":"6","value":"0.0"},{"time":"2019-08-07","no":"7","value":"0.0"},{"time":"2019-08-08","no":"8","value":"0.0"},{"time":"2019-08-09","no":"9","value":"0.0"},{"time":"2019-08-10","no":"10","value":"0.0"},{"time":"2019-08-11","no":"11","value":"0.0"},{"time":"2019-08-12","no":"12","value":"0.0"},{"time":"2019-08-13","no":"13","value":"0.0"},{"time":"2019-08-14","no":"14","value":"0.0"},{"time":"2019-08-15","no":"15","value":"0.0"},{"time":"2019-08-16","no":"16","value":"0.0"},{"time":"2019-08-17","no":"17","value":"0.0"},{"time":"2019-08-18","no":"18","value":"0.0"},{"time":"2019-08-19","no":"19","value":"0.0"},{"time":"2019-08-20","no":"20","value":"0.0"},{"time":"2019-08-21","no":"21","value":"0.0"},{"time":"2019-08-22","no":"22","value":"0.0"},{"time":"2019-08-23","no":"23","value":"0.0"},{"time":"2019-08-24","no":"24","value":"0.0"},{"time":"2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}
Try this
<?php
$json_url = file_get_contents('Your url goes here');
$data = json_decode($json_url,true);
var_dump($data);
// You can print all of your data here to see if it works
foreach ($data as $items) {
// You can loop trough the data and get it like $items->sid etc
var_dump($items);
}
?>
You need to grab the URL with 'file_get_contents' and than you need to decode it with json_decode
and then you print it out with the for each function

How to access laravel sessions variables in payu post request

I'm having some problems trying to access variables sessions in the confirmation page used it by payu. In the post request I can access the variables sent from the gateway like this: $state_pol = $_POST["state_pol"]; and no problem with that, I can stored the value in my data base, But when I try to do this $email = Auth::user()->email; when I do that I can't get any variable or stored values in the DB. I need to stored in the DB the cart Items and I'm using moltin Cart, so I need to do something like this:
foreach (Cart::contents() as $item) {
$citem = new Ite;
$citem->compra_id = $compra->id;
$citem->id_product = $item->id;
$citem->name = $item->name;
$citem->val_uni = $item->price;
$citem->image = $item->image;
$citem->iva = $item->tax;
$citem->qty = $item->quantity;
$citem->val_total = $item->total();
$citem->save();
}
Cart::destroy();
the methods Cart::contents() and Cart::destroy(); don't work, but just in the payu request, I tried this with paypal request and everything works correctly and I'm using the same code, I mean with the variables session.
Thanks in advance for the time and help.
I just realized that the problem is with eloquent too. I mean, I cannot get access to the DB, I cannot do something like this $user = User::where('id','=',1)->first(); I tried with query builder but it didn't work.

get request for a website in a php script

I want to get request in a website like that
$id = "something";
$response = http_get("http://example.com?id=$id, array("timeout"=>1), $out);
I don't find how exactly I can use http_get
I've tried with file_get_contents, it is working like
$out = file_get_contents("http://www.example.com?id=something");
but I want it like that
$out = file_get_contents("http://www.example.com?id=$id");
this one is not working
The way I got it to work was using the code below.
$out = file_get_contents("http://www.example.com?id=".$id);
This will concatenate the $id variable contents onto the end of the url to request.

PHP - Display results from this 'Detect' array?

re: Home Site = http://mobiledetect.net/
re: this script = Mobile_Detect.php
Download script here: https://github.com/serbanghita/Mobile-Detect
This script functions perfectly detecting the different parameters of a user's device.
However, this is how I am currently detecting these parameters:
// each part of the IF statement is hard-coded = not the way to do this
if($detect->isiOS()){
$usingOS = 'iOS';
}
if($detect->isAndroidOS()){
$usingOS = 'Android';
}
echo 'Your OS is: '.$usingOS;
My goal is to use a FOREACH to iterate thru the various arrays in this script to determine a user's device's parameters. I would need to have the "($detect->isXXXXOS())" be dynamic... (which, would be based upon the KEY). The results would display the KEY. But the detection would be based upon the VALUE.
Also, since my web page uses a REQUIRE to access this script... in the Mobile_Script.php script, the arrays are "protected." I think this is also causing me problems (but I don't know for sure).
Any help is appreciated.
In foreach loop you can call dynamic method look like this :
$array = array('Android','Windows','Linux','Mac');
foreach( $array as $value) {
$method = "is{$value}OS";
if($detect->$method()) {
$os = $value;
echo "Your OS is : {$os}";
}
}
Please rearrange your code what you want. I give you an example.
you can try to use somethin like this:
$OSList = $detect->getOperatingSystems();// will give array of operating system name => match params
foreach($OSList as $os_name=>$os_params/*unused*/)
{
$method = 'is'.$os_name;
if($detect->$method())
{
$usingOS = $os_name;
}
}

Send Data from Drupal to node.js

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.

Categories