file_get_contents is not work when my parameter is long - php

i want to send parameters to url with GET method , i used file_get_content to do this.
my code is :
$app_url = "http://localhost/index.php?number=%s&service_id=%s&shortcode=%s&msg=%s" ;
$msg = "Thank you for joining us! خوش آمدید";
$msg = urlencode($msg);
$address = sprintf($app_url , $current_user,$service_id,$scode,$msg);
$ret = file_get_contents($address);
echo $ret
it works , and dont have problem , but when my $msg is long, i get warning message and parameters don't send to url:
Warning: file_get_contents(http://localhost/send_sms/index.php?number=123&service_id=test&shortcode=123&msg=Thank+u%%%%s%21%0A%D%%%%%8%B4%D9%85%D8%A7+%%%%%%D8%A8%D8%A7+%D9%85%%%%D9%%%%88%D9%81%D9%8

Related

Remote mail() function

I am trying to use my localhost to connect to another server remotely and execute a mail function so i use get method to send the email and the subject
here is my localhost file :
<?php
for ($email=0; $email< $n_emails; $email++){
$urlsmtp = $link.'?email='.$destino.'&subject='.$tornado.'&msg='.$msgrand.'&name='.$_POST['naming'].'&from='.$_POST['localting'];
//this link is something like http://test.com/RemoteSmpt.php?email=test#mail.com &subject=test&msg=test&name=test&from=test
$urlsmtp = str_replace(" ", "%20", $urlsmtp);
$response = file_get_contents($urlsmtp);
}
?>
and the file RemoteSmpt.php which i will put it in the server contains this code :
<?php
if ( isset( $_GET['email']) && $_GET['subject'] && $_GET['msg'] && $_GET['name'] && $_GET['from']) {
$email = $_GET['email'];
$subject = $_GET['subject'];
$msg = $_GET['msg'];
$name = $_GET['name'];
$from = $_GET['from'];
$headers = 'From: '.$name.'<'.$from.'>\n';
$enviar = mail($email, $subject, $msg, $headers);
if ($enviar){
echo ('<font color="green"> -'. $email .' 0k -- With Subject : '.$subject.'</font><br>');
} else {
echo ('<font color="red"> -'. $email .' Not Sended -- With Subject : '.$subject.'</font><br>');
}
}else
{echo 'Invalid Data !<br>';}
?>
so after i upload the RemoteSmpt.php file and try this is what i got :
Warning: file_get_contents(http://...#mail.com&subject=tset&msg=test&name=test&from=test): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in Mylocalhost file !
Please any idea how to fix this code !
You need to properly encode special characters in the URL parameters, which can be done using urlencode().
You can use the http_build_query() function to build a query string from an associative array, and properly encode everything.
$params = http_build_query([
'email' => $destino,
'subject' => $tornado,
'msg' => $msgrand,
'name' => $_POST['naming'],
'from' => $_POST['localting']
]);
$urlsmtp = $link . '?' . $params;

Sodium Crypto box seal open not working in PHP

So I'm trying to get libsodium's sodium_crypto_box_seal and sodium_crypto_box_seal_open working but for some reason, the open is failing and I can't work out why.
So in all my trying to get this working, I have built a test system that a single PHP file that tests how it would work cross server.
<pre>
<?php
/*** Client Sending ***/
// saved argument
$remotePublic = "DXOCV4BU6ptxt2IwKZaP23S4CjLESfLE+ng1tMS3tg4=";
// create out key for this message
$key = sodium_crypto_box_keypair();
// encrypt our message using the remotePublic
$sealed = sodium_crypto_box_seal("This is a test", base64_decode($remotePublic));
$send = json_encode((object)array("pub" => base64_encode(sodium_crypto_box_publickey($key)), "msg" => base64_encode($sealed)));
echo "Sending : {$send} \r\n";
/*** Server Setup ***/
$payload = json_decode($send);
$apps =
array (
'test' =>
array (
'S' => 'lv/dT3YC+Am1MCllkHeA2r3D25HW0zPjRrqzR8sepv4=',
'P' => 'DXOCV4BU6ptxt2IwKZaP23S4CjLESfLE+ng1tMS3tg4=',
),
);
/*** Server Opening ***/
$msg = $payload->msg;
$key = sodium_crypto_box_keypair_from_secretkey_and_publickey(base64_decode($apps['test']['S']), base64_decode($apps['test']['P']));
$opened = sodium_crypto_box_seal_open(base64_decode($msg), $key);
echo "Opened : {$opened} \r\n";
/*** Server Responding ***/
$sealedResp = base64_encode(sodium_crypto_box_seal("We Got your message '{$opened}'", base64_decode($payload->pub)));
echo "Responding : {$sealedResp}\r\n";
/*** Client Receiving ***/
$received = sodium_crypto_box_seal_open(base64_decode($sealedResp), $key);
echo "Received : {$received}\r\n";
/*** Sanity Checking ***/
if($received == "We Got your message 'This is a test'"){
echo "Test Successfull.\r\n";
}else{
echo "Test Failed got '{$received}' is not \"We Got your message 'This is a test'\"\r\n";
}
?>
</pre>
Output is:
Sending : {"pub":"DS2uolF5lXZ1E3rw0V2WHELAKj6+vRKnxGPQFlhTEFU=","msg":"VVYfphc2RnQL2E8A0oOdc6E\/+iUgWO1rPd3rfodjLhE+slEWsivB6QiaLiMuQ31XMP\/1\/s+t+CSHu8QukoY="}
Opened : This is a test
Responding : cvDN9aT9Xj7DPRhYZFGOR4auFnAcI3qlwVBBRY4mN28JmagaR8ZR9gt6W5C0xyt06AdrQR+sZFcyb500rx6iDTEC4n/H77cUM81vy2WfV8m5iRgp
Received :
Test Failed got '' is not "We Got your message 'This is a test'"
There's two problems here.
First -- in this step under "Server Opening":
$opened = sodium_crypto_box_seal_open($msg, $key);
$msg is still Base64 encoded, so trying to decrypt it will fail.
Second -- the public key that is included in the "pub" field of $send is the public key of a random keypair that was generated by sodium_crypto_box_keypair(), not the same public key as $remotePublic or the pair in $apps. This key is overwritten by a call to sodium_crypto_box_keypair_from_secretkey_and_publickey() later in the application, making the original message unrecoverable.

How to Send via email the Entire Request in PHP

I'm trying to create a php that sends via email all possible information about the request. Currently I'm using something like:
Email request php:
<?php
$remoteIp = $_SERVER['REMOTE_ADDR'];
$remoteHost = $_SERVER['HTTP_HOST'];
$remoteRef = $_SERVER['HTTP_REFERER'];
$remoteUrl = $_SERVER['REQUEST_URI'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$yourEmailAddress = "user#mail.com";
$emailSubject = "New request from: ".$remoteIp;
$emailContent = "The URL Request was made to: $remoteUrl
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent";
// send the message
mail($yourEmailAddress, $emailSubject, $emailContent);
?>
I like to add the Entire http request (GET OR POST) to the email been send so $emailContent it look like this:
$emailContent = "The URL Request was made to: $remoteUrl
The request REFERER was: $remoteRef
The IP was $remoteIp
The User Agent was: $userAgent"
The Full request was:
$fullrequest";
Looking around I found this https://gist.github.com/magnetikonline/650e30e485c0f91f2f40 which allows you to create file but I'm not sure on how to put it together with my PHP so it sends me an email with the request. (Creating the file is not necessary)
This PHP I like to integrate to my Email request php
<?php
// https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
class DumpHTTPRequestToFile {
public function execute($targetFile) {
$data = sprintf(
"%s %s %s\n\nHTTP headers:\n",
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_SERVER['SERVER_PROTOCOL']
);
foreach ($this->getHeaderList() as $name => $value) {
$data .= $name . ': ' . $value . "\n";
}
$data .= "\nRequest body:\n";
file_put_contents(
$targetFile,
$data . file_get_contents('php://input') . "\n"
);
echo("Done!\n\n");
}
private function getHeaderList() {
$headerList = [];
foreach ($_SERVER as $name => $value) {
if (preg_match('/^HTTP_/',$name)) {
// convert HTTP_HEADER_NAME to Header-Name
$name = strtr(substr($name,5),'_',' ');
$name = ucwords(strtolower($name));
$name = strtr($name,' ','-');
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
Can any one help me to add class DumpHTTPRequestToFile to $fullrequest?
Something like
<?php
$message="The following request was made:\n";
foreach($_REQUEST as $k=>$v){
$message.=$k." : ".$v."\n\n";
}
mail($to_address, $subject, $message, $headers);
?>
Replace $_REQUEST with the super global of your choice. Or run similar loops across multiple superglobals ($_SERVER, etc) if $_REQUEST doesn't have all you want in it.

Mandrill PHP library - FROM field

How can I set a proper FROM field for the PHP Mandrill library?
In Java, I can use:
from = "Some Description <noreply#domain.com>";
If I try the same in PHP, I get an error:
Validation error: {"message":{"from_email":"The username portion of the email address is invalid (the portion before the #: Some Description
Only emails with FROMs like this get through:
$from = "noreply#domain.com";
In case it matters, here's how I send an email:
$from = "Some Description <noreply#domain.com>";
$message = array("subject" => $aSubject, "from_email" => $from, "html" => $aBody, "to" => $to);
$response = $mandrill->messages->send($message, $async = false, $ip_pool = null, $send_at = null);
If you read the documentation https://mandrillapp.com/api/docs/messages.html it has two parameters in request
from_email and from_name when we pass
$from = "Name <email>",
It is not accepted by mandrill hence it throws error , To pass a name you need to pass from_name with name value.

Telegram bot send file and text PHP

I've got a basic bot set up that will send text back to the user, now I also want to send the user an audio message. But that I cannot do, here is the code.
I'm also using this https://github.com/Eleirbag89/TelegramBotPHP to send the audio
include("Telegram.php);
define('BOT_TOKEN', 'tokentoken');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
$telegram = new Telegram(BOT_TOKEN);
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$reply = sendMessage($message);
$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".$reply;
file_get_contents($sendto);
function sendMessage(&$string) {
switch ($string) {
case "Hi":
$message = "Hi Back";
sendAudio();
break;
case "Bye":
$message = "Bye Bye";
break;
default:
$message = "Default";
}
return $message
}
func sendAudio() {
$sound = curl_file_create('sampleAudio.mp3', 'audio/mp3');
$newContent = array('chat_id' => $chatID, 'audio' => $sound);
$telegram->sendAudio($newContent);
}
Calling the code outside of the functions works, but than the user gets the file each time they type something. I experimenting so a bit of explanation would be great.
You have some errors:
First line " is unclosed
Last function, you've typed "func" instead of "function"
In sendAudio() you've used $chatID but you've to pass it as a parameter of sendAudio() or to set is as global.

Categories