I have a variable called $filter. If I run echo $filter; I get the contents of it. However, If I run
file_put_contents("/nrj/vvr/tmp/lol", $filter);
I do get the file, but the contents is just 1. It should contain alot of different things. I use the exact same syntax usually, and it works. I get no error messages.
This line was the problem
$filter = curl_exec($ch);
For some reason, it ignores the first things and just executes it without setting any variable. Under this I had
echo $filter;
Giving me the impression that it was that line that output it, but it was the one above.
Related
I was just studying and messing around with PHP, when i encountered something that, to me, doesn't make much sense, but i could be missing something.
So, i have this spreadsheet file that i load on my system, and i want to print every data that is inside the file. I load the file, and turn it into an array, like this:
$file = $request->spreadsheet;
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
$spreadsheet = $spreadsheet->getActiveSheet();
$data_array = $spreadsheet->toArray();
So far so good. Now, i want to print everything that is inside this file, but i wanted to display is using it's attributes, something like:
foreach($data_array as $data){
$x = json_decode($data[2]);
echo $x->nome;
echo $x->telefone;
}
I'm accessing $data[2] because its the third position of the array that contains all the info about the user, like name, telephone or whatever.
The thing is, if i run the code like i've just showed, i get the error "Trying to get property 'nome' of non-object", but if i try to echo the EXACT same thing, but outside the "foreach", like this:
foreach($data_array as $data){
$x = json_decode($data[2]);
}
echo $x->nome;
echo $x->telefone;
I have no error at all, and shows me the info perfectly, so.. what is going on ? lol
The property of a non-object error inside the foreach means this particular row either has no JSON or is invalid JSON. Your last snippet working OK means the last row has valid JSON.
It's possible the first row is column header, for example "Data" which of course will not parse as JSON and will trigger the error.
Look at your spreadsheet rows to narrow it down, and you can output var_dump($data[2]) in the foreach.
I'm having a problem where my PHP scripts are returning my json encoded array with a number above and a number below it. Like follows:
26
[0,"edited_token_string"]
0
I have not changed any of the scripts that I'm encountering this on, but it is happening with all of them. I don't have any other echos other than the one used to echo the array. Our server was returning "null" from all of the scripts all morning and now is returning the correct array, with these numbers surrounding it. Is it possible something was updated on the server that accidentally turned on some type of debugging? I've called our hosting service, but they are incredibly unhelpful.
Thanks in advance,
Max
Try adding header('Content-type: application/json'); directly above the line that outputs your JSON. If something else has already outputted something, you will get an error telling you where in your code that happened.
For the undesired output after the JSON, could it be that there is also an extra space after the closing ?> tag? A quick and dirty fix would be to just add die; after the last intentional echo;
I know this is probably old-hat for many of the good folks on this forum, but after a few hours of hunting here for a solution I'm still having trouble.
I have confirmed that I can get a simple call to file_get_contents() to work:
$content = file_get_contents('example.com');
print $content;
(Outputs the contents of the site, as expected.)
And, if I plug one of the URLs generated by my script into the browser directly, that works too:
http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=/netahtml/PTO/search-adv.htm&r=0&p=1&f=S&l=50&Query=AN/"allied signal" AND ((((((((CCL/29/$ OR CCL/62/$) OR CCL/165/$) OR CCL/180/$) OR CCL/236/$) OR CCL/237/$) OR CCL/241/$) OR CCL/248/$) OR CCL/417/$)&d=PTXT
(In actuality, that is generated/stored as a literal string named $url in my code. Though, I'm noticing as I type this that the quotes are throwing off something here. Not sure if that translates to my problem or not.)
But, when I try to combine the two it fails immediately:
$content = file_get_contents($url);
print $content;
(Outputs nothing.)
I've tried every suggestion I've seen so far involving various uses of cURL instead, and of course json to try to get some clue what is happening. But, other than the one case where I got HTTP 400 to spit out, and another where the output was NULL (sorry, I lost the links to the specific posts I'd been reading), no luck there.
I'm sure its something ludicrously simple that I'm missing here, that I'll probably ::head desk:: when I find it, but right now I'm stuck. Any suggestions?
Thanks In Advance
EDIT: I forgot to include, nothing in the error log either.
EDIT: allow_url_fopen is set correctly.
EDIT: Use of urlencode($url) does produce the following error, when the example above is called...
[29-May-2014 02:08:57 America/New_York] PHP Warning: file_get_contents(http%3A%2F%2Fpatft.uspto.gov%2Fnetacgi%2Fnph-Parser%3FSect1%3DPTO2%26Sect2%3DHITOFF%26u%3D%2Fnetahtml%2FPTO%2Fsearch-adv.htm%26r%3D0%26p%3D1%26f%3DS%26l%3D50%26Query%3DAN%2F%22allied+signal%22%0D%0A+AND+%28%28%28%28%28%28%28%28CCL%2F29%2F%24+OR+CCL%2F62%2F%24%29+OR+CCL%2F165%2F%24%29+OR+CCL%2F180%2F%24%29+OR+CCL%2F236%2F%24%29+OR+CCL%2F237%2F%24%29+OR+CCL%2F241%2F%24%29+OR+CCL%2F248%2F%24%29+OR+CCL%2F417%2F%24%29%26d%3DPTXT): failed to open stream: Invalid argument in C:\inetpub\wwwroot\PHP-CGI\test.php on line 21
EDIT: Some test code...
<?php
print "Testing...<BR>";
define("PATFT_head",'http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&u=%2Fnetahtml%2FPTO%2Fsearch-adv.htm&r=0&p=1&f=S&l=50&Query=AN%2F%22');
define("PATFT_foot",'%22+AND+%28%28%28%28%28%28%28%28CCL%2F29%2F%24+OR+CCL%2F62%2F%24%29+OR+CCL%2F165%2F%24%29+OR+CCL%2F180%2F%24%29+OR+CCL%2F236%2F%24%29+OR+CCL%2F237%2F%24%29+OR+CCL%2F241%2F%24%29+OR+CCL%2F248%2F%24%29+OR+CCL%2F417%2F%24%29&d=PTXT');
$assignees = file("./config/assignees.txt");
foreach ($assignees as $name) {
$url= PATFT_head.$name.PATFT_foot;
$content = file_get_contents($url);
#print "Should see the page here...<BR>";
print $content;
}
print "<BR>Done.";
?>
Contents of assignees.txt is a series of search terms (in this case the Assignee Name of a patent holder), 1 per line.
Try urlencode :
$content = file_get_content(urlencode($url));
print $content;
There is a setting for allow_url_fopen in the PHP INI. Make sure you set this to true or 1.
Let's say I have a file "English.txt" containing these lines :
$_LANG["accountinfo"] = "Account Information";
$_LANG["accountstats"] = "Account Statistics";
Note : the file extension is .txt and there is nothing I can do to change that. There is no opening PHP tag (<?php) or anything, just those lines, period.
I need to extract and actually get the $_LANG array declared from these lines. How do I do that? Simply includeing the file echoes every line, so I do
ob_start();
include '/path/to/English.txt';
$str = ob_get_clean();
Now, if I call eval on that string, I get an syntax error, unexpected $end. Any ideas?
Thanks.
eval(file_get_contents('English.txt'));
however, be sure NOBODY can change English.txt, it could be dangerous!
First of all, note that you should use file_get_contents instead of include with output buffering. Since it contains no <?php tag, there is no need to run it through the script processor.
The following works perfectly in my tests:
<?php
$contents = file_get_contents("English.txt");
eval($contents);
var_dump($_LANG);
As one of the comments said, if you do the above and still get an error, then your file does NOT contain exactly/only those lines. Make sure the file is actually syntax compliant.
As has been mentioned, you should really use eval only as a last resort, and only if the file is as safe to execute as any code you write. In other words, it must not be editable by the outside world.
I have a string that has HTML & PHP in it, when I pull the string from the database, it is echo'd to screen, but the PHP code doesn't display. The string looks like this:
$string = 'Hello <?php echo 'World';?>';
echo $string;
Output
Hello
Source Code
Hello <?php echo 'World';?>
When I look in the source code, I can see the php line there. So what I need to do is eval() just the php segment that is in the string.
One thing to consider is that the PHP could be located anywhere in the string at any given time.
* Just to clarify, my PHP config is correct, this is a case of some PHP being dumped from the database and not rendering, because I am echo'ing a variable with the PHP code in it, it fails to run. *
Thanks again for any help I may receive.
$str = "Hello
<?php echo 'World';?>";
$matches = array();
preg_match('/<\?php (.+) \?>/x', $str, $matches);
eval($matches[1]);
This will work, but like others have and will suggest, this is a terrible idea. Your application architecture should never revolve around storing code in the database.
Most simply, if you have pages that always need to display strings, store those strings in the database, not code to produce them. Real world data is more complicated than this, but must always be properly modelled in the database.
Edit: Would need adapting with preg_replace_callback to remove the source/interpolate correctly.
You shouldn't eval the php code, just run it. It's need to be php interpreter installed, and apache+php properly configured. Then this .php file should output Hello World.
Answer to the edit:
Use preg_replace_callback to get the php part, eval it, replace the input to the output, then echo it.
But. If you should eval things come from database, i'm almost sure, it's a design error.
eval() should work fine, as long as the code is proper PHP and ends with a semicolon. How about you strip off the php tag first, then eval it.
The following example was tested and works:
<?php
$db_result = "<?php echo 'World';?>";
$stripped_code = str_replace('?>', '', str_replace('<?php', '', $db_result));
eval($stripped_code);
?>
Just make sure that whatever you retrieve from the db has been properly sanitized first, since you're essentially allowing anyone who can get content into the db, to execute code.