Correct syntax for php inside a feed request - php

I have a very basic query string which passes a ID to a receiving page.
On that page, I need to dynamically call the YouTube API, giving my playlistID.
I'm having to use PHP for this, and it's a little out of my comfort zone, so hopefully someone can wade in with a quick fix for me.
Here is my variable
$playlist;
And I need to replace the 77DC230FBBCE4D58 below with that variable.
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/77DC230FBBCE4D58?v=2';
Any help, as always, greatly appreciated!

Once the $playlist variable is set you can construct the feed URL as :
$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
or
$feedURL = "http://gdata.youtube.com/feeds/api/playlists/$playlist?v=2";

$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/'.rawurlencode($playlist).'?v=2';
Or perhaps a little neater:
$feedurl = sprintf(
'http://gdata.youtube.com/feeds/api/playlists/%s?v=2',
rawurlencode($playlist)
);
(Note: rawurlencode is used just in case [not that it's likely with YouTube playlist IDs] the $playlist value contains any funky characters.)
More infos:
String concatenation with the . operator
Encoding potentially "unsafe" URL characters with rawurlencode
Adding values to "formatted strings" with sprintf

$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';
you use a full stop to join strings in PHP

Related

base64_decode showing corrupted characters

I am building a hyperlink that includes a base64 encoded set of parameters as shown below:
$params = base64_encode("member_id={$recipient_id}&api_key=".SECRET_KEY);
$link = HOST_ADDRESS."test.php?k=" . $params;
When the link is executed, the following code runs:
// get the encoded string from the link parameter
$link_parm = $_GET['k'];
$link = substr($link_parm, 0);
// url encode the string to ensure all special characters convert properly - attempt to stop errors
urlencode($link);
// decode the rest of the link
$decoded_link = base64_decode($link);
// get the remaining data elements from the link parameter
$msg_data = preg_split( "/[&=]/", $decoded_link);
On occasion, the $msg data is corrupted and looks like this:
member_id=167œÈ&api_key=secretkey
As you can see the member id is corrupted.
Can someone please help me understand what may be causing this?
Thanks.
For starters, there are a few problems with this beside the issue you describe.
What are you trying to do using $link = substr($link_parm, 0);? This could just be written as $link = $link_parm;. Or, you could of course just do $link = $_GET['k']; or even just use $_GET['k'].
urlencode($link); does nothing as you're not catching its result. The argument is not passed by reference.
Your "attempt to stop errors" should probably be handled differently. By throwing an error when you're receiving unexpected input, for instance.

How to delete tracking code from links in PHP

Hi I have a form in WordPress where users can submit a link to a product, but very often the links come with unnecessary baggage, like tracking codes. I would like to create a filter in WordPress and clean the links so they consist of just a working link. I would like to if possible confirm that the link still works or a method that will guarantee that the link will still work.
The main things I want to get rid of in links are utm_source and it's contents, utm_medium and it's contents, etc. Everything but the clean working link.
So for example, a link like this:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam
Will end up like this:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
I'd really appreciate if someone can lead me in the right direction.
Thanks!
You can do what you want with explode, parse_str and http_build_query. This code uses an array of unwanted parameters to decide what to delete from the query string:
$unwanted_params = array('utm_source', 'utm_medium', 'utm_campaign', 'clickId', 'publisherId', 'source', 'pdp', 'details', 'fo_k', 'fo_s');
$url = 'https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055&pdp=true&source=detail&utm_source=affiliate&utm_medium=affiliate&utm_campaign=pjdatafeed&publisherId=20648&clickId=2669312134#fo_c=745&fo_k=c0ebaf8359ca7853df8343e535533280&fo_s=pepperjam';
list($path, $query_string) = explode('?', $url, 2);
// parse the query string
parse_str($query_string, $params);
// delete unwanted parameters
foreach ($unwanted_params as $p) unset($params[$p]);
// rebuild the query
$query_string = http_build_query($params);
// reassemble the URL
$url = $path . '?' . $query_string;
echo $url;
Output:
https://www.serenaandlily.com/variationproduct?dwvar_m10055_size=Twin&dwvar_m10055_color=Chambray&pid=m10055
Demo on 3v4l.org
You can do this in the PHP itself. There is a function called parse_url() (https://secure.php.net/manual/en/function.parse-url.php) which can give you all the URI params as array. After parsing, you can filter the parameters, remove the unwanted. Finally, use http_build_query() (https://secure.php.net/manual/en/function.http-build-query.php) to build a string URI to return :)

Premium url shortner issue with urlencode replacing & sign with ampersand

As a novice and beginner php learner, I'm using the Code-Canyon Premium URL Shortner script and done 2 days of research. Unfortunately I am unable to resolve my issue.
The url shorten script is urlencoding the API url that it sends to the script, In doing this it is replacing the & symbols with & causing the url to not work correctly on the final destination page.
I have tried to use preg_replace, str_replace and also tried to use urldecode on the destination page but none of these seem to work. Here is my current script:
$makeshort = "http://mywebsite.com/email/quote.php?quoteid=$visitor&customertype=fhbs";
$mkshrt = str_replace("/&/","%26",$makeshort);
$short = "http://shorturl.com/api?&api=REMOVED&format=text&url=".urlencode($mkshrt);
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res)
$shorturl = $res;
$shorty = json_decode($shorturl);
$shorturl = $shorty->{'short'};
echo $shorturl;
Note: Where you see &format=text in the api url, I have tried to use it with and without the &format=text however this makes no difference what so ever.
I am hoping that there could be a simple and quick way to resolve this issue as I am only passing over 2 variables and its the second variable that is being displayed like this:
mywebsite.com/email/quote.php?quoteid=01234567890&customertype=fhbs
So the customertype variable is the one being messed up due to the amp; symbol.
I sincerely hope someone with the expertise could advise me on the best approach or even a simple way to resolve this issues as I really am at my whits end! MY knowledge is not good enough to research the exact key phrases in order to point myself in the right direction.
Thanks for your time in reading this and I hope someone would be kind enough to help me out here.
I know the feeling as i myself am just becoming to terms with coding and developing.
I personally would solve this by one of two ways, If you have tried to already use htmlspecialchars or htmlentities along with urldecode then the most simple and quickest way to achieve this would be to read the URL string then replace the &symbol with the & using str_replace and do either a meta refresh of the page or `header location redirect
Here is what i mean with a breif example however one must stress that some extra security maybe needed and this is ONLY a quick fix not a secure stable and permanent fix, Though one could play with this and maybe work something out for your own circumstances.
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
echo "<meta http-equiv='refresh' content='0;URL=$url'>";
exit;
}
Alternative way with header location:
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(strstr($url, "&")){
$url = "http://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url = str_replace('&', '&', $url);
header("Location: $url");
exit();
}
This will totally remove any & symbols from the url and replace them with &.
You can also play around with this to remove even more from the url string and replace things like / or forbidden words.
An example of the output will look like this:
Original url causing the problems:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
New url after the script has executed and refreshed the page:
http://mywebsite.com/email/quote.php?quoteid=1234567890&customertype=fhbs
As you can see from the hyperlinked text above, The ampersand breaks the string and everything after that is not read correctly but when this script executes and refreshes the page the url will be just like the second hyperlink thus making the url work for what you require.
NOTE: THIS IS NOT A SECURE WAY OF DOING THINGS AND MAY NO BE IDEAL FOR YOUR CIRCUMSTANCES, THIS IS JUST AN IDEA AND HOPE THIS HELPS!
Thanks.

$_GET, Ampersand is interrupting

Trying to use a $_GET['url'] variable to grab data from a URL:
http://mysite.com/?url=http://this.is/?q=an&?example=url
What I want above is bolded, but sadly the $_GET['url'] will only get "http:// this.is/?q=an" because the & makes it interpret it as the beginning of a new variable within the URL.
Is there a way to ignore the ampersands so my script can get the entire URL I need it to? The URL that is appended to ?url= is not within my limits to control so most work but some do contain the dreaded &. After reading questions on Stack Overflow I'm not holding out much hope :(
If you have absolutely no control over the arguments placed on the query string (for whatever reason), you can also do this by manually parsing the $_SERVER['QUERY_STRING'] varible, e.g.
$page = str_replace("url=", "", $_SERVER['QUERY_STRING']);
Of course, if possible, you should encode it using the answers posted by everyone else.
Use urlencode()
$get_url = urlencode('http://this.is/?q=an&?example=url');
$url = 'http://mysite.com/?url=' . $get_url;
If you can't control the query string, you could use
$query = $_SERVER['QUERY_STRING'];
$pos = strpos($query, "url=");
if ($pos !== false) {
$url = substr($query, $pos + 4);
}
This code returns everthing after url=
If you have the possibility,you should encode the url with urlencode to create a clean url.
If you don't do this, you get eventually an server error, with strange urls and you can't pass more than one url(because everything after the url= is interpreted as url)
Here is the tested code:
if (!empty($_GET['url'])) {
echo $_GET['url'].'<br />';
}
$url = urlencode('http://this.is/?q=an&?example=url');
echo 'LINK';
Hope it will help you.

file_get_contents sometimes does not find texts

I'am new to PHP, so please be nice :)
Sometimes file_get_contents does its job and sometimes not. I have build a simple check for URLs, if they exist, on webpages. But the problem is, that even the URL exist for sure (manually checked the sourcecode), file_get_contents and preg_match do not find it. I can't figure out why. Here's the code:
$page = $URL_that_should_be_checked;
$checkurl = str_replace("/", "\/", $checkurl);
$page = file_get_contents($userpage);
$checkurl = "/".$checkurl."/";
$report = preg_match($checkurl, $page);
Thank you very much!!
Try this:
$page = file_get_contents($userpage);
$report = preg_match('/' . preg_quote($checkurl) . '/', $page);
It's rather difficult to follow your code.
Did you check the documentation page and note that special characters may need to be encoded with the urlencode() function?
On this line $checkurl = str_replace("/", "\/", $checkurl); the variable $checkurl does not appear to have a definition.
On this line $userpage does not seem to be defined. Only $page is defined in the code you've provided.
It looks like you're doing a lot of work to set up a preg_match and $report its value. It's unclear why you need to fetch the page during this process.
Also, do you have allow_url_fopen set to true? Are you getting any error messages?
You should use the PHP Function preg_quote to parse the URL to your preg_match Function. Example:
$checkurl = preg_quote($checkurl);
See: http://php.net/manual/de/function.preg-quote.php
Not sure what your code is doing.
You do
$page = $URL_that_should_be_checked;
but two lines later do
$page = file_get_contents($userpage);
without ever having set $userpage.

Categories