PHP file to string [duplicate] - php

This question already has answers here:
How can I read a php file into a string using php?
(2 answers)
Closed 7 years ago.
Basically I want to run a PHP file from another PHP file to get the output as a string. I also want to be able to pass variables into it.
For example:
$result = get_output('./otherfile.php', $vars);
I have no idea how to do it. any help would be really appreciated.

Try reading this and file_get_contents
$contents = file_get_contents('http://www.stackoverflow.com/');
echo $contents;

You have two files, 'included.php' and 'launch.php':
included.php
<?php echo $hello;
launch.php
$hello = 'hi';
ob_start();
include('included.php');
$returned = ob_get_contents();
ob_end_clean();
In '$returned' you have stored the results of the 'included.php' code
(try to launch var_dump($returned));
As you can see, you can pass variables creating them before the inclusion of included.php. With ob_start you start to save the output of the page and then, with ob_get_contents, you put the result in a variable.

Related

is there a way to reestructure file_get_contents output? php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Im triyng to take some information from a json file,
the code in that page is like this
{"asignaciones":[{"fecha":"LUNES
29/07","horaEntrada":"18:30","horaSalida":"22:30","tienda":" BK Villa
Urquiza"}],"fechaConsulta":"29/07/2019 17:27","legajo":"28907"}
i want to take "29/7", "18:30", "22:30".
For now, i can print all the code in my page, but i want to take only those numbers, there is a way with:file_get_contents?
i'm trying to learn a little more php sorry if this question is oviously simple.
my code now:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
echo $content
?>
It looks like the content is a json... Start from this:
$content = file_get_contents('http://proveedores.alsea.com.ar:48080/asignaciones-server/mobile/main/asignaciones/legajos/28907');
$content = json_decode($content);
print_r($content);

How to remove file url when calling var_dump() function [duplicate]

This question already has answers here:
Why does var_dump show filename and line number?
(2 answers)
Closed 5 years ago.
so here in the below code im calling nytimes api
<?php
function rpnyt_article_get_result( $rpnyt_search , $rpnyt_key ){
$rpnyt_url = 'https://api.nytimes.com/svc/search/v2/articlesearch.json?q='.$rpnyt_search.'&api-key='.$rpnyt_key ;
$json_feed = wp_remote_get($rpnyt_url);
var_dump($json_feed[ 'body']);
}
?>
im getting response back as expected but that includes file url from where im calling this function like /home/ubuntu/XXXXXXXXX/xxxxxxxxxxxx/plugins/XXxxx/includes/rpnyt-news-content.php:8:(see image)
Try var_export() instead of var_dump().
http://php.net/manual/en/function.var-export.php
Consider using
echo(json_encode($your_thing));

How do I get the end of a file path in php? [duplicate]

This question already has answers here:
How to get the last dir from a path in a string
(11 answers)
Closed 6 years ago.
So I have a file path as a text variable in php:
"one/cool/file/path"
I need a php code to return "path"
By return I think you mean display...
Lets suppose you have the following path:
"one/cool/file/path"
Stored as a string in the variable:
"path"
To return it (or display it) you can just do:
echo path;
If you then go to your php in a web browser you should see your parh there!
You could achieve this using the following method:
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');
echo $path_parts['dirname'], "\n";
Output: /www/htdocs/inc
http://php.net/manual/en/function.pathinfo.php#example-2657

fetch file with file_get_content and insert text into placeholder variables [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Problem when loading php file into variable (Load result of php code instead of the code as a string)
I am trying to create a template based application and I am having issues writing the user data into a pre-existing template file.
I would initially use file_get_contents('template.php');
and template.php would contain the following:
echo $user;
Is there anyway to insert data into a placeholder variable in a template file using file_get_contents?
I suppose I could use DomDocument or regex to insert the data into a placeholder string, but would this be possible to do with a php variable?
If you have control over template.php and are aware of the security considerations, you're probably looking for include or require:
include('template.php');
Will do what you want to do.
EDIT
Output buffering
ob_start();
include('template.php');
$result = ob_get_clean();

PHP code to get the HTML code of a webpage [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I get the HTML code of a web page in PHP?
Is there a PHP code to get the HTML code of a webpage?
$html = file_get_contents('https://stackoverflow.com/questions/ask');
this doesnt work. It simply loads the webpage. I want to display the HTML code when I run the code.
Thanks in advance.
Try:
echo htmlentities(file_get_contents('http://stackoverflow.com/questions/ask'));
Just HTML encode your output, so that & becomes &, using the htmlentities function.
Perhaps try:
$html = htmlspecialchars(file_get_contents('http://stackoverflow.com/questions/ask'));
and try using the tags to output it.
$handle = #fopen("'http://www.webmasterworld.com", "rt");
$code = fread($handle,9000);
This is copied form another thread, but it should be good enough. It will get the code in $handle and then $code would contain 9000 bytes of that code.
Try using exec and wget together:
<?php
exec('wget http://stackoverflow.com/questions/ask -O', $array);
echo implode('<br />', $array);
?>

Categories