how to pass variable value in URL via file_get_contents - php

I want to integrate SMS to my API and try to pass the variables in URL but it says error, no number found.
Please help
$sender = "9999708993";
$reply_message = "This is an auto generated message";
echo $sender;
echo $reply_message;
$lines = file_get_contents('http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender');
echo "<pre>$lines</pre>";

use double quotes rather than single quotes if you want to put variables in:
$lines = file_get_contents("http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender");
Please try this:
$sender = "9999708993";
$reply_message = urlencode("This is an auto generated message");
//I think you need to change these
$loginid='myid';
$mypassword='mypassword';
$mysenderid='mysenderid';
$uri = "http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=$myid&password=$mypassword&senderid=$mysenderid&message=$reply_message&number=$sender";
var_dump($uri);
$lines=file_get_contents($uri);
var_dump($lines);

try cleaning the URL, it might have a new line at the end
trim("$URL");

Related

How to use echo in url

i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";

PHP variables in HTML string

I'm receiving some information from the AJAX form, and I need to generate a HTML email that will use some information from this form.
For example, I want to send something basic like
$msg = '<html><body>
<p>Bla-bla-bla $information</p></body></html>'
How can I insert $information variable into paragraph without concatenation (using it is pretty painful, as the email HTML has pretty complex structure)?
If you want to add vars into variable without concat then maybe you are looking for something like this
$information = "Info";
$msg = "<html><body>
<p>Bla-bla-bla $information</p></body></html>";
use your double quotes
Be aware if your var $information is array like this:
$information['info'] = "Info";
$information['id_info'] = 1;
$msg = "<html><body>
<p>Bla-bla-bla $information[info]</p>
<a href\"somepage.php?id=$information[id_info]\">Link</a></body></html>";
Double and single quotes
$email = 'someone#example.com';
$var = 'My email is $email'; // My email is $email
$var = "My email is $email"; // My email is someone#example.com
There are diffent ways to do it:
1) You can use "."
For example:
$msg = '<html><body>
<p>Bla-bla-bla'. $information.'</p></body></html>';
2) You can use double quotes:
$msg = "<html><body>
<p>Bla-bla-bla $information</p></body></html>";

Unable to Parse JSON Fields Due to Leading Bracket in PHP

I'm trying to create a webhook for Mandrill that will send an e-mail to the sender when a previously sent e-mail bounces. I'm able to receive the JSON data from Mandrill, but am unable to parse that data to send to the original sender:
<?php
require_once 'mandrill-api-php/src/Mandrill.php'; //Not required with Composer
$mandrill = new Mandrill('*myapikey*');
$json = stripslashes($_POST['mandrill_events']);
$jsondata = json_decode($json,true);
$subject = $jsondata['event'];
$message = "STRIPSLASHES: ".$json."----JSONDATA----".$jsondata;
$emailAddress = "*me#mydomain.com*";
mail($emailAddress, $subject, $message);
?>
Here is what the $json data looks like in the $message variable. It is a literal copy and paste from the test e-mail I receive:
STRIPSLASHES: [{"event":"spam","msg":{"ts":1365109999,"subject":"This an example webhook message","email":"example.webhook#mandrillapp.com","sender":"example.sender#mandrillapp.com","tags":["webhook-example"],"opens":[{"ts":1365111111}],"clicks":[{"ts":1365111111,"url":"http://mandrill.com"}],"state":"sent","metadata":{"user_id":111},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa","_version":"exampleaaaaaaaaaaaaaaa"},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa","ts":1422475458},{"event":"spam","msg":{"ts":1365109999,"subject":"This an example webhook message","email":"example.webhook#mandrillapp.com","sender":"example.sender#mandrillapp.com","tags":["webhook-example"],"opens":[{"ts":1365111111}],"clicks":[{"ts":1365111111,"url":"http://mandrill.com"}],"state":"sent","metadata":{"user_id":111},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1","_version":"exampleaaaaaaaaaaaaaaa"},"_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1","ts":1422475458}]----JSONDATA----Array
I notice that the $json is outputting the json data, but has a leading and ending bracket, as opposed to starting with a squiggly bracket. So I decided to call the data as if it were an array, but to no avail.
In a test, instead of doing $json = stripslashes(... I copy and pasted the json data above as a literal string. Once I removed the leading/ending brackets, I was able to parse some data.
Why don't you try removing the brackets using PHP?
$json = ltrim($json, "[");
$json = rtrim($json, ']");
And then pass it to the decoder?
Actually, I was able to fix it by "grabbing" the json data a different way, formatting it correctly as I receive it:
$rawdata = file_get_contents('php://input');
$decodeurl = urldecode($data);
$jsonready = substr($decodeurl, 16);
$data = json_decode($jsonready, true);
$recipient = $data['0']['msg']['email'];
//etc, etc, etc
I followed this example:
https://sendy.co/forum/discussion/1137/using-mandrill-webhook-for-bounces-complaints/p1
I hope this helps people who are trying to utilize Mandrill's API!

Getting data from facebook xml

I have a problem, I'm trying to get some data for a unique link to my site.
When people are viewing eg: video.php?id=23 i want the script to get the data for that site using $_GET['id'].
Here's my script, and I've tried everything. Hope you can help me!
<?php
$vidurl = $_GET['id'];
function fb_count() {
global $fbcount;
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=$vidurl');
$fbbegin = '<share_count>'; $fbend = '</share_count>';
$fbpage = $facebook;
$fbparts = explode($fbbegin,$fbpage);
$fbpage = $fbparts[1];
$fbparts = explode($fbend,$fbpage);
$fbcount = $fbparts[0];
if($fbcount == '') { $fbcount = '0'; }
}
fb_count();
?>
The problem is that it wont let me print the $vidurl, it doesnt seem to work, because it is only getting data from the following link : fniis.dk/video.php?id= and not eg: fniis.dk/video.php?id=123
You have a couple of problems in your code.
First, you won't be able to access $vidurl in fb_count() unless you specify it as global inside fb_count():
global $vidurl;
It is recommended that you pass $vidurl as a parameter fb_count() instead of using global.
Second, your concatenation of $vidurl in file_get_contents is incorrect. You should be using double quotes instead of single quotes so $vidurl will be processed by PHP. It also wouldn't hurt to use urlencode() here:
// note: using single quotes here and just concatenating with "."
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=' . urlencode($vidurl));
That should get your code working.

XML Address Trouble

I have this xml that I want to insert some info, the code is working fine but when it comes to the address of the XML I'm having some trouble.
I have a link in other page with:
produtoadicionado.php?page=adicionar&cod_produto=1&id=1&nome_produto=Arroz
What I want is complete the name of the XML with the value of $id before. Which in this case is 1.
So it will call and add in: 1_produtos.xml
But is not working. Is creating a new xml named $id_produtos.xml
produtoadicionado.php
<?php
$page = $_GET["page"];
$cod_produto = $_GET["cod_produto"];
$id = $_GET["id"];
$nome = $_GET["nome_produto"];
if ($page == 'adicionar')
{
$xml = simplexml_load_file('$id_produtos.xml');
$produto = $xml->addChild('produto');
$produto->addChild('nome', $nome);
$produto->addChild('cod', $cod_produto);
file_put_contents('$id_produtos.xml', $xml->asXML());
}
?>
Please help me!
Change these lines:
simplexml_load_file('$id_produtos.xml');
file_put_contents('$id_produtos.xml', $xml->asXML());
into this:
simplexml_load_file("{$id}_produtos.xml");
file_put_contents("{$id}_produtos.xml", $xml->asXML());
Please, note the use of double quote instead of the single quote which let PHP to interpret the vars name and replace that with their value. You can read more here.

Categories