URL Error 2912 in fopen($url, "r") - php

I receive "error 2912" when I request a URL through fopen($url, "r") in PHP
Example:
It is actually on an sms API. I initiated some variables to substitute for some required fields.
$url = api.smartsmssolutions.com/smsapi.php?username=myusername&password=mypassword&sender=".$sender."&recipient=".$d3."&message=".$const_msg;

As per error 2912 your Recipient is empty. The correct format given is
http://api.smartsmssolutions.com/smsapi.php?username=YourUsername&password=YourPassword&sender=SenderID&recipient=234809xxxxxxx,2348030000000&message=YourMessage
Check by changing the url as below
$username = "YOUR_USERNAME";
$password = "YOUR_PASSWORD";
$sender = "YOUR_ID";
$d3 = "2348922223,2569878987";
$const_msg = "YOUR_MESSAGE";
$url = "http://api.smartsmssolutions.com/smsapi.php?username={$username}&password={$password}&sender={$sender}&recipient={$d3}&message={$const_msg}";

Error code 2912=Recipient is empty.
Please check correct value send by parameter.

The problem was that the variables were not initiated properly.
I had to use UrlEncode() in parsing the variables to the fopen($url).
Like This.
$urlt = "http://api.smartsmssolutions.com/smsapi.php?username=".UrlEncode($username)."&password=".UrlEncode($password)."&sender=" . UrlEncode($sender)
."&recipient=234" . UrlEncode($d3)
.",&message=" . UrlEncode($const_msg);

Related

cant understand why my post request using Postman receive the param only if i use x-www-form-urlencoded

i'm try to send some post value using postman to my script php... but i can print out the value post only if i use the x-www-form-urlencoded and not if i send data via form-data?
why this? can't understand differance between x-www-form-urlencoded and form-data.
php code test:
<?php
// this script call to register new user into db
require ("../private/index.php");
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$staff = $_POST['staff_ID'];
$email = $_POST['email'];
$pass = $_POST['password'];
$name = $_POST['Name'];
$surname = $_POST['Surname'];
$role = $_POST['Role'];
$instructor = $_POST['Instructor'];
$date = $date = date("Y-m-d H:i:s",time());
$dataReceived = array('staff_ID'=>$staff,
'email'=>$email,
'password'=>$pass,
'Name'=>$name,
'Surname'=>$surname,
'Role'=>$role);
echo json_encode($dataReceived);
$sql = "INSERT INTO `users_nx`(`staff_ID`,`password`,`email`,`Name`,`Surname`,`Role`) VALUES ($staff,$pass,$email,$name,$surname,$role)";
if ($mysqli->query($sql) === TRUE) {
echo resultOperations(true, $mysqli);
} else {
echo resultOperations(false,$mysqli);
}
}
it just so happens that I've studied this before. Both x-www-form-urlencoded and form-data are carried in the request body during transmission.
First of all, x-www-form-urlencoded is generally used for the submission of forms, such as multiple input fields, and form-data is used for the submission of files, such as file files, image images and so on. The former is a string in the format of json key-value, while the latter is a binary stream.
Why can't I get the data in the php server? I don't know php, I've tested it in nodejs before, and nodejs requires an npm called bodyparser to format the string key to a json key-value. php should come with this parsing process. So you can get the json data directly. However, if you are using form-data, the binary cannot be parsed according to the parsing rules. It needs to be obtained by stream parsing.
This may be a bit of a fluff, but you can just print the data for the http request body to see the difference.

How to deal with unencoded URL redirects to my website correctly?

We are using CleverReach to redirect people to our website after they have double opt-in their mail account. We redirect the email as a query parameter to our website, like: example.com/thanks?email=foo#bar.com (by setting up a redirect in the CleverReach backend like example.com/thanks?email={EMAIL}). Apparently, the email parameter doesnt get urlencoded by cleverreach.
Now, in Drupal, if the URL is like so: example.com/thanks?email=hello+world#bar.com and using this code:
$request = \Drupal::request();
$email = $request->query->get('email');
$email is hello world#bar.com. Now, I dont know what the correct processing is here. Obviously, I cant tell CleverReach to urlencode their redirects beforehand. I dont even know if that would be best practice or if I need to imlement something...
The only thing I found out is that $_SERVER['QUERY_STRING'] contains the "real" string, which I can urlencode and then redirect, and then, by reading the query params, urldecode them. But I feel like I am missing some crucial inbuilt functionality.
TL;DR
If a website redirects to my website using not urlencoded query params, how do I read them?
My current approach:
<?php
public function redirectIfIllegalUri() {
$request = \Drupal::request();
$email = $request->query->get('email', '');
$needsRedirect = (false !== strpos($email, ' ') || false !== strpos($email, '#'));
if ($needsRedirect && isset($_SERVER['QUERY_STRING']) && false !== strpos($_SERVER['QUERY_STRING'], 'email=')) {
$sqs = $_SERVER['QUERY_STRING'];
$sqs = htmlspecialchars($sqs);
$sqs = filter_var($sqs, FILTER_SANITIZE_STRING);
$sqs = filter_var($sqs, FILTER_SANITIZE_ENCODED);
$sqs = urldecode($sqs);
$sqs = explode('&', $sqs);
foreach ($sqs as $queryParam) {
if (false === strpos($queryParam, 'email=')) continue;
$values = explode('=', $queryParam);
$email = $values[1];
}
$emailEncoded = urlencode($email);
$query = $request->query->all();
$query['email'] = $emailEncoded;
$refreshUrl = Url::fromRoute('<current>');
$refreshUrl->setOptions([
'query' => $query,
]);
$response = new RedirectResponse($refreshUrl->toString(), 301);
$response->send();
return;
}
}
$request = \Drupal::request();
$email = urldecode($request->query->get('email', false));
drupal request() docs
The problem you are facing is that the + will be treated as a space when you get the value from $_GET global variable.
Currently in PHP doesn't exist a method that returns these values without urldecoding and you need to build a custom function to achieve what you are asking:
A simple function will return not encoded input is by using this function:
function get_params() {
$getData = $_SERVER['QUERY_STRING'];
$getParams = explode('&', $getData);
$getParameters = [];
foreach ($getParams as $getParam) {
$parsed = explode('=', $getParam);
$getParameters[$parsed[0]] = $parsed[1];
}
return $getParameters;
}
This solution can be used if you do not have any other option. By using this function you will always get the data encoded.
If you can encode the value from cleverreach then the best approach is to encode it there.
Encoding the value in cleverreach for email hello+world#bar.com will give you this url example.com/thanks?email=hello%2Bworld%40bar.com and in $_GET you will have the email containing the + sign.

Syntax or Variable error in PHP

Is there any problem with this PHP code? It displays nothing. Please help.
<?php
date_default_timezone_set('Asia/Kuala_Lumpur');
$date_time = date('Y-m-d H:i:s');
$host = $_SERVER['HTTP_HOST'];
$device = Detect::deviceType();
$device_brand = Detect::brand();
$operating_system = Detect::os();
$browser_name = Detect::browser();
$service_host = Detect::ipHostname();
$service_org = Detect::ipOrg();
$ip_country = Detect::ipCountry();
echo $host;
echo $date_time;
echo "IP: ". $ip_country;
?>
1) Your script is calling Detect class, but I don't see from where it comes. Are you including a file containing its declaration?
2) Is Detect class "mute", ie, works without verbose response?
3) If you are not getting any errors, try to add to your script first lines
error_reporting(2047);
ini_set("display_errors",1);

How to pass data from textboxes to PayPal using CodeIgniter

I am trying to make a PayPal transaction in my page. I have the following fields defined:
firstname
lastname
address
email
amount
When I click to proceed to payment it redirects my browser to PayPal correctly. Afterwards I am able to log in using the test account and make a test payment. The problem is that when I call print_r to display the return_url to check the data that has been passed, I noticed that my input textbox wasn't there.
Here is the code that posts to PayPal:
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$address = $this->input->post('address');
$email = $this->input->post('email');
$config['business'] = 'test#test.com';
$config['return'] = 'http://localhost/test/payment/return_details/';
$config['cancel_return'] = 'http://localhost/test/payment/create-job-listing/';
$config['production'] = FALSE; //Its false by default and will use sandbox
$config["first_name"] = 'asdasd';
$config["last_name"] = 'asasas';
$config['address1'] = 'new york';
$this->load->library('paypal',$config);
$this->paypal->add('Amount',0.1);
$this->paypal->pay(); //Proccess the payment
Can someone help me figure this out?
In your return_url put the code:
<?php
echo "<pre>";
$data = file_get_contents('php://input'); //this fetches the raw input stream
$info = explode('&',$data);
echo "<pre>";
print_r($info);
?>
//Information sent by paypal is in the input stream and this code fetches the input!!

Save variable as variable in MYSQL for later use

I have an email template which i am saving in database. My problem is some part of message are variable means these data are coming from current user data.
For Example My Template is
$message="This is test for $username. I am sending mail to $email."
here $username and $email is coming from current users and it is varying from user to user.
So problem is how to save it in database so i can use it as variable on php page later.
anybody have any idea please help me.your help would be appreciated.
If you really need to store whole template in database, you can save it using your own created constants e.g. [USERNAME], [EMAIL] and then in php script just use str_replace() on them.
$messageTemplate = 'This is test for [USERNAME]. I am sending mail to [EMAIL].';
$message = str_replace(array('[USERNAME]', '[EMAIL]'), array($username, $email), $messageTemplate);
But you can also divide this string and concatenate it with variables from database as follows:
$message = 'This is test for ' . $username . '. I am sending mail to ' . $email . '.';
You can use something like this:
$input = "This is test for {username}. I am sending mail to {email}.";
$tokens = array("username" => $username, "email" => $email);
$tmp = $input;
foreach($tokens as $key => $token)
{
$tmp = str_replace("{".$key."}", $token, $tmp);
}
echo $tmp;
The variables in the string will not be evaluated as variables automatically just because you are adding it to your php scope. You need to eval the string in order for the variables to be replaced:
$username = 'test';
$email = 'test#test.com';
$str = "This is a test for $username. I am sending mail to some person $email.";
echo $str. "\n";
// This is a test for $username. I am sending mail to some person $email.
eval("\$str = \"$str\";");
echo $str. "\n";
// This is a test for test. I am sending mail to some person test#test.com.
For more information, see http://php.net/manual/en/function.eval.php

Categories