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.
Related
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.
I got a problem I cant solve for 7 hours now.
this is my php script for creating user on openfire server:
$f = fopen("LINK","r");
$odpoved = fread($f, 1024);
in manual http://www.igniterealtime.org/projects/openfire/plugins/userservice/readme.html is written that my variable should contain either "OK" or "UserAlreadyExistsException". When i try to print $odpoved, it show good, when i view source of that page it contain tags as written in manual. My problem is, that i cant for gods sake find a way to use this variable in switch. This is how it looks now:
switch($odpoved){
case 'OK':
print("something");
break;
case 'UserAlreadyExistsException':
print("something2");
break;
default:
print("X");
}
fclose($f);
No matter what i do, it always print default. I dont understand what is wrong with it. I tried already to compare it to options with tags too, but it didnt help and it always end up in default.
Would you kindly help me please? Thanks for any help and if needed I will provide additional details.
The server will reply to all User Service requests with an XML result page. If the request was processed successfully the return will be a "result" element with a text body of "OK". If the request was unsuccessful, the return will be an "error" element with a text body of one of the following error strings.
The output is an XML file, so what you're looking at is using an XML parser to get the result element. simplexml will load it into an array (since this xml page is small you shouldn't have to worry about performance so more complicated xml parsers aren't worth the trouble)
$output = simplexml_load_string($odpoved)
switch($output->result) //check $odpoved to make sure the XML structure matches
Or, you can also pass the url using simplexml_load_file
the output will be the same.
for more information, see: http://www.sitepoint.com/parsing-xml-with-simplexml/
Your switch statement looks fine to me. I would question the content in your variable $odpoved. Use var_dump($odpoved) to see what it contains and be sure to check for leading and trailing spaces or hidden characters (newline chars are sometimes hard to spot).
For switch statement analysis check out http://www.php.net/manual/en/control-structures.switch.php
PHP part:
$php = $_POST['php'];
//$php = "print \"hello world\";";
if ($php != null){
if (strlen($php) < 400){
echo $php;
eval($php);
//eval("print \"hello world\";");
}else die ("Evaluated Expression Exceeds Maximum Length");
}
LSL part:
string php = "print \"hello world\";";
Now I added the commented out bits into PHP to show that it works. And then when the LSL script sends to PHP it returns:
print \"hello world\"; -- this line is from, 'echo $php;'
<b>Parse error</b>: syntax error, unexpected '"', expecting identifier
(T_STRING) in <b>xxxxxx.php(141) : eval()'d code</b> on line <b>1</b><br />
-- this is the error.
And it is something to do with the the way the two scripts are sending data. I thought maybe had something to do with $php = $_POST['php']; so changed it to $php = $_POST[php]; With no change to the result. I then tried changing print \"hello world\"; to print 'hello world'; It then just returns the error : T_ENCAPSED_AND_WHITESPACE.
I did not supply the full source here. Only the section that was having an issue. It is supplied in a example state. The output is the same as the actual error result, that is being seen in the source. Usage of eval is required for the lsl script and php. In that the code is dynamically being reconfigured by both and sent to one another. Essentially giving the two the ability to code into one another. This is for a game in Second Life.
So if anyone knows of an actual way to pass the required data to and from the scripts. I could use some advice. Or a smack in the head if I missed something simple.
With the kind poke from mario on turning off magic_quotes. I then found what the data was doing in the source. I then ended up researching and using the following : eval(stripslashes($php)); Which completely solves the issue. And based on marios poke.
It had nothing to do with escape data. Didn't think so as echo reported that fine. And it was indeed a slap me in the head error too.
stripslashes — Un-quotes a quoted string
Will vote this as best answer and also a best answer for mario. Wish he would have done his as a answer over comment. So could have voted it.
$file = simplexml_load_file($url); {
foreach($file->entry as $post) {
$row = simplexml_load_string($post->asXML()); // after adding this line, i get error message
$links = $row->xpath('//link[#rel="alternate" and #type="text/html"]');
echo (string) $post->title;
echo (string) $links[0]['href'];
I use this script to parse atom feed. At first didn't work because it couldn't pass the link's href attribute properly. I added $row and even though it worked, it gives an error : "namespace prefix gd for etag on entry is not defined". I'm searching this for hours, can't find a solution. I was so close.
The line $row = simplexml_load_string($post->asXML());, if it worked, would be a long-winded way of writing $row = $post. ->asXML() and simplexml_load_string perform the opposite action to each other so you'd get back the same object you started with.
I think the reason it's behaving strangely in your case is that your XML document is using "namespaces", and the fragment of XML produced by $post->asXML() doesn't quite work as an XML document on its own.
I suspect that the original problem you had, which this line seemed to magically fix, was also with namespaces, as XPath is rather sensitive to them. Look up examples of using registerXPathNamespace and see if they solve your problem. If not, feel free to post a follow-up question showing your original problem, and including a sample of the XML you're processing.
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.