Create and include a variable in SimplePie (PHP) - php

Within the code below, how do I set VAR as some variable and then include it within the SimplePie code as the feed URL? The "feed" code is from the Simplepie PHP libary.
<?php
$VAR = "http://website.com/feed/";
$feed1 = new SimplePie();
$feed1->set_feed_url('$VAR');
$feed1->init();
?>

I m not sure about Simple Pie. But if you use single quotes it wont be processed. Try double quotes or better you dont need any quotes for that.
Try this :
$feed1->set_feed_url($VAR);

Related

PHP not using full URL?

This is my code:
<?php
$url = "https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27";
$xml = simplexml_load_file($url);
?>
Im trying to get this .XML file into my $url. The problem is that its using
https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT and not the one with the filter:
https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27
Why is it not taking the URL i entered there? When i open the URL in my browser i get the filtered one but when i run the code itll give me the other .XML.
I hope someone can help me.
You are using double quotes, so php will interpret $filter as a (probably undefined...) variable.
You can avoid that using single quotes:
$url = 'https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT?$filter=Kenteken%20eq%20%2701GBB1%27';
Use single quotes.
Then re-read the docs
Have you tried this?
<?php
$url='https://api.datamarket.azure.com/opendata.rdw/VRTG.Open.Data/v1/KENT_VRTG_O_DAT? $filter=Kenteken%20eq%20%2701GBB1%27';
$xml = new SimpleXMLElement(file_get_contents($url));
?>

Adding PHP code in HTML which is already in PHP? [duplicate]

This question already has answers here:
Simple PHP stuff : variable evaluation [duplicate]
(5 answers)
Closed 11 months ago.
Basically, I want to add a PHP variable in HTML, with that HTML already being inserted into a constant, which is in PHP code. Here's what I mean: (obviously this code below is wrong, but imagine I would want to be inserting the $VARIABLE in the URL of the iFrame, for example)
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
?>
What would be the syntax for adding that variable in there?
Thanks in advance!
This is just a basic template. str_replace() should do the trick.
const EXAMPLE = "<iframe src='http://example.com/{{{VARIABLE}}}'></iframe>";
$variable = 'example-sub-category
$merged_content = str_replace('{{{variable}}}', $variable, EXAMPLE);
Note I used {{{}}} to denote the insert. This is not PHP syntax, but you will find templates often use something like that the would not be expected in the text otherwise to denote placeholders.
Have you tried:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/".$VARIABLE."'></iframe>";
?>
You can insert PHP anywhere in HTML just by defining its tags <?php ?>
<iframe src='http://example.com/<?php echo $VARIABLE; ?>'></iframe>
Using your example:
const EXAMPLE = "<iframe src='http://example.com/" . $VARIABLE . "'></iframe>";
For this kind of task I'd better use sprintf:
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/%s'></iframe>";
echo sprintf(self::EXAMPLE, $VARIABLE);
To move in and out of php within your html, use <? [php code here...] and ?> to end php and continue with html. You can escape characters with a back slash \ and using single quotes as Rujikin mentioned above. Or, within PHP, you can echo your html so that you're not bouncing in and out of php.
For example:
some php...
echo '<span style=\"color:#980000\"><strong>\"{$searchTerm}\"</strong></span>';
more php...
Notice that the html is enclosed in single quotes (end-to-end) and inside the single quotes, double quotes are used where required by html, e.g., for the <style> tag.
This example is basically saying, "echo whatever the value of php variable $searchTerm is, put it in quotes that's required by echo, and make it dark blue (#0000FF) and bold (<strong>).
I hope this helps. :)
You can try something like:
<?php
$VARIABLE = 'example-sub-category';
const EXAMPLE = "<iframe src='http://example.com/$VARIABLE'></iframe>";
$newHtml = str_replace('$VARIABLE', $VARIABLE);
?>
As you mentioned your code was fake, so I guess what you need is to replace some places in your HTML strings by a content which is unknown until you php script is executed, right ?
so what you need is to put some place holders and change them later, as I showed in my snippet above.
Hope this works

PHP: Trying to gt a part of url to INTO PHP

Okay so I have my PHP code: (I need help with the 4th line) More info down below the code.
Basicly I am trying to get code below. i am trying to put get part of url but it doesn;'t work
$oIMDB = new IMDB('<?ph p echo$_GET("m");?> ')
and on the 4th line i put the code
and the code doesn't work, how can i use it?
I think you mean this:
$oIMDB = new IMDB($_GET["m"]);
you shouldn't need the php scripting block inside the class call. Function parameters don't need quotes either unless you are using a literal string. Try
$oIMDB = new IMDB($_GET["m"])

Help in fpdf imade display

Hello every one i m using fpdf libray to creat pdf files from html form.
i m using
$pdf->Image('C:/DOCUME%7E1/mypic.PNG',60,140,120,0,'','');
to display image on pdf.
in its first parameter it asks for exact path.it doesn't accept anyaddress variable here.
but i want to make dynamic.i have able to get a complete path in an variable.
i have printed this variable.
//////////////////////////////
echo "$path";
output
////////////////////
C:/DOCUME%7E1/mypic.PNG
/////////////////////////////////////////////////////
but how i put that path from variable in this parameter.?
when i use this variable as in this function.it give error.
$pdf->Image('$path',60,140,120,0,'','');
plz help me for this.
$pdf->Image($path,60,140,120,0,'','');
This should work.
I removed the single quotes from the variable.
OR
put the $path in double quotes:-
$pdf->Image("$path",60,140,120,0,'','');
Remove the single quotes wrapping the variable name.
The single quotes make PHP treat your string as a literal (i.e. $path instead of C:/DOCUME%7E1/mypic.PNG).
Confusingly, it would work with double quotes because of variable interpolation.

using php include in jquery

What im trying to do, is use php include within a jquery append attribute. something like this:
$('a.popup[href^=#]').click(function() {
$('body').append('<div id="content" class="popup_block"><?php include( SITE_URL . 'activity/popup.php' ) ?></div>');
My script is in a php file, server side, so that i could accomplish this, but im not sure how to go about it. When it comes to html, css etc. I can combine it and php within the php file, but when it comes to javascript, its the quotes that confuses me, and when and how to use the brackets. This might sound confusing lol. Anyways, does CDATA have anything to do with it? I've never used it before, but I should atleast learn it's use.
The PHP interpreter will only look for <?php and ?> tags and try to evaluate anything in between. It doesn't care about surrounding quotes. You need to make sure though that the result of whatever PHP does is valid Javascript.
var foo = '<?php include 'foo.php'; ?>';
becomes
var foo = 'This is the content of foo.php.';
after PHP is done with it.
If there are any quotes in foo.php, it may become this:
var foo = 'This is the 'content' of foo.php.';
which is invalid Javascript syntax. You'll need to escape any character of foo.php that may cause such invalid syntax, for example with addslashes. This can be quite cumbersome though, so I'd advise to look for an alternative this to begin with.
You can encode the value using JSON, which is definitely syntax safe:
var foo = <?php echo json_encode("Some string with 'quotes'."); ?>;
Generating code in code is always tricky, try to not do it and stick to language neutral data interchange formats like JSON or XML.
If you are 100% sure you don't have any single quotes in your include, there should be no problems with how you have it.
If you want to visualize it, copy all of your generated code from the included php file and paste it right into the main page inside of the append(). See how it looks. This will give you a good idea of what the browser will end up with.

Categories