Dynamically passing value to sms api in php? - php

This is my sms url
"https://example.com/api/sendhttp.php?authkey=#############&mobiles=".$mobile."&message=".urlencode($rem)."&sender=abcdef&route=#"
Here is my Code
<?php
session_start();
$mobile=98994564564;
$rem="Hi";
$url=$_SESSION['smsurl'];
echo $url;
?>
What I am trying to do here is I am calling this url through session variable. But when I am displaying the url, it is not getting value of $mobile and $rem.
It should display
"https://example.com/api/sendhttp.php?authkey=#############&mobiles=8994564564"&message=".urlencode("Hi")."&sender=abcdef&route=#"

The problem is, $_SESSION['smsurl'] does not contain the actual variables like $mobile or $rem, instead it contains the value of those variables. So you can't simply do $mobile=ABC; $rem="XYZ"; $url=$_SESSION['smsurl']; to change the query part of the URL.
The solution is,
Parse the URL
Change the query part of the URL
Reconstruct the URL again
So your code should be like this,
<?php
session_start();
$mobile=98994564564;
$rem="Hi";
$urlArray = parse_url($_SESSION['smsurl']);
parse_str($urlArray['query'], $qStringArray);
$qStringArray['mobiles'] = $mobile;
$qStringArray['message'] = urlencode($rem);
$qString = http_build_query($qStringArray);
$apiUrl = $urlArray['scheme'] . '://' . $urlArray['host'] . $urlArray['path'] . '?' . $qString;
// Display the final URL
echo $apiUrl;
?>

Related

Preventing adding link to another link

Hello I'm currently working with php to generate a menu with a own build CMS system.
I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:
as example =
$row_menu['page_link'] = page2;
$url . $row_menu['page_link'];
it will return as example : http://example.com/page2
But when I click again, it adds page2 again like : http://example.com/page2/page2
How do i prevent this?
Thanks in advance!
Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.
Instead you can use HTTP_REFERER like
$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];
Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like
$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
REQUEST_URI will give you whatever comes after example.com, so leave that out all together.
$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];
You can find a full list of the $_SERVER references here.
Try this:
$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);
$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
array_pop($uri_segments);
}
$uri = implode('/',$uri_segments);
$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];
echo $url;

Output complete link in each href

Im using Curl with simple html dom to scrape a website and in order to fix relative links I insert a base tag like this:
foreach($html->find('head') as $f) {
$f->innertext = "<base href='$url'>" . $f->innertext;
}
Where $url is the website Im scraping. The problem is that the links are physically outputted like this:
link
While I need the full url in the link like so:
link
How can I achieve this?
append the url each time you are setting it.
$base_url = "http://www.somewebsite.com/";
foreach($html->find('head') as $f) {
$f->innertext = "<base href='$base_url$url'>" . $f->innertext;
}
Try to get the base URL like this:
<?php
$baseURL = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>
then prepend $baseURL to your href

Wordpress custom field echoing whole URL

My code is as below:
<?php
if( get_field( "facebook" ) !== '' ): ?>
Facebook
<?php endif;?>
Instead of echoing the field's value which is (wwww.facebook.com), it's echoing it relative to the wordpress website.
Also, is my code efficient? Or is there a simpler way to do it?
Edit: What finally worked for me:
<?php
$website = (get_field('website'));
if(!empty($website)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $website))? 'http://'.$website: $website;
echo "$final_url" . "<br />";
}
?>
you should add http:// on the beggining to make external URLS
Facebook
or add http:// on your advanced custom field in the admin
EDIT:
here is your final code:
$url = the_field('facebook');
if($url!=""){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $url))? 'http://'.$url: $url;
echo 'Facebook<br/>';
}
NOTE:
your data wwww.facebook.com has excess w
i appended the code given by #feeela so it would check if http:// is present, thanks to #feeela

URL Replacement in PHP

I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;

PHP variable-infused link not writing to a variable?

http://www.reecemcmillin.com/albums/
<?php
$uncut = file_get_contents('http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q=' . $_POST['band'] . '+' . $_POST['album'] . '+zip+inurl:mediafire');
$strip1 = strstr($uncut, 'www.mediafire.com/?');
$link = substr($strip1, 0, 30);
echo $link;
?>
It doesn't seem to be writing the website content to $uncut. Can somebody help me figure out what's wrong? Thanks.<3
Clients are not supposed to send URI-fragments (the portion of the URI following #) to servers when they retrieve a document. PHP is probably sending a request for the google homepage, effectively: file_get_contents('http://www.google.com/');. If you echo $uncut, that's probably what you'll see you're getting back.
Try a querystring-based URI instead.
<?php
$uncut = file_get_contents('http://www.google.com/search?sclient=psy-ab&hl=en&safe=active&source=hp&q=' . urlencode($_POST['band']) . '+' . urlencode($_POST['album']) . '+zip+inurl:mediafire');

Categories