php equivalent of python lxlm.etree fromstring - php

I'm fairly new to python and I'm re-writing a python script that accesses an API.
In the python script, I'm trying to find the equivalient of this:
response = get_resource(url, auth=(username,password), params=params)
return lxml.etree.fromstring(response.content)
returns <http://www.w3.org/2005/Atom} feed at 0x2f12b70>
What is the equivalent of this code in php? And I'm not even sure what this is returning?
Thanks.

Related

How to pass PHP variable values to Python via shell_exec

I have some variables defined in a PHP file. I then call a python script from the PHP with these variables as arguments. HOWEVER the values of the variables do not carry over to my python script, it seems as though they as passed as strings and not as variables:
$first = "doggy";
$second = "kitty";
$command = escapeshellcmd('python ./script.py $first $second');
$output = shell_exec($command);
The above code produces not "doggy" and "kitty" respectively in my Python script, but literally "$first" and "$second". I want doggy and kitty.
For example when I print in Python:
print sys.argv[1];
>>$first
is the output I am receiving.
My Python script is NOT outputting anything, the script interacts with an API that I wish to use these variables with.
I have tried these previous posts which seem to be near what I am asking. I am either not understanding them, or they are not working. Some answers are too technical or too vague.
Passing value from PHP script to Python script
PHP <-> Python variable exchange
If shell_exec is not the best way for this, I am open to new ideas. Any and all help is appreciated. Thanks for taking the time to look at my post.
A single quoted string will be displayed literally, while a double quoted string will interpret things like variables and new line sequences (\n).
Therefore, change from single to double quotes:
$command = escapeshellcmd("python ./script.py $first $second");
Read more about strings in the PHP manual: http://se1.php.net/manual/en/language.types.string.php

How to filter user' s input ( html, with PHP backend) with python?

There is a web application written in PHP and HTML. What I want is to filter a users input for a variety of cases and sanitize it. For example, I want to compare the input from a form (string) with a list of allowed strings and depending if it is right or wrong to trigger the suitable PHP function to handle this.
My question is how to bind the user input with the python script and then the outcome of this python script as an input for PHP?
thanks
You can call the Python script from your PHP file as a shell command, passing it JSON-formatted arguments. Then have the Python script output the response (also JSON encoded) and have the PHP file capture that. Here's an example I used recently, cobbled together from the links below:
PHP file:
$py_input = ... // Your data goes here.
// Call the Python script, passing it the JSON argument, and capturing the result.
$py_output = shell_exec('python script.py ' . escapeshellarg(json_encode($py_input)));
$py_result = json_decode($py_output);
Python file:
import json
php_input = json.loads(sys.argv[1]) # The first command line argument.
# Do your thing.
php_output = ... # Whatever your output is.
print json.dumps(php_output) # Print it out in JSON format.
Passing a Python list to php
executing Python script in PHP and exchanging data between the two

PHP - Syntax of exec() function to call another php file

This question is in reference to:
Free (preferably) PHP RTF to HTML converter?
I'm trying to execute that last line of code in my php:
exec(rtf2htm file.rtf file.html)
I understand what parameters need to go within the parentheses, I just do not know how to write it. I've looked at multiple examples along with the php documentation and still I remain confused, so could someone show me how it is written? rtf2htm refers to a PHP file which converts RTF to HTML.
Ultimately what I am trying to do is convert the content of numerous RTF docs to HTML, maintaining the formatting, while not creating tags such as<head> or <body> which programs like Word or TextEdit generate when converting to HTML.
rtf2htm is not a php script, it is a program installed on the server. exec() is used to call external applications.
EDIT: After looking up this script, it seems that it is indeed a php script. But it has been coded to be usable from the command line only.
This should work:
<?php
exec('php /path/to/rtf2htm /path/to/source.rtf /path/to/output.html');
?>

Receive hash from Perl Script to use in PHP

I have a perl script that does a lot of config file parsing for me and creates a hash with all the information I need.
I want to call that script from PHP and have PHP get the hash to be able to work with the hash in php and not just returning some html code from the perl script.
Is that possible? Haven't found any way yet and just know that I am able to return lots of html code as output, but that's not what I want the perl script to do.
The simplest way, serialize this hash into json in perl and print resulting string to STDOUT.
In PHP it can be easily decoded into array or object...
If the platform that is executing the PHP allows for it, you can call the exec() function to execute external files like:
$result = exec( "/path_to/your_script.pl", $lines, $state);

Using C/C++ code in PHP without writing an extension

I read it's possible to use C/C++ code in PHP by writing an extension. Is there a simpler way, like wrapping magic or things like that (I must say, I heard the word, but don't know much about what a wrapper is).
Compiling a php extension isn't very hard. There is a little API to learn just like C programs has the main function and how to pass variables to the main function.
No there is not.
PHP parses PHP not C or its decendants like C++
If you want to include C code in php like some function written in C then it has to be called in an extension and it has to be compiled.
A wrapper is code around code. Most any language you use like Delphi, Vb etc. have had native code created that then calls an external API function and in the process handles any type conversion required or parameter fix up.
Among others of the same kind, tcc can be used as a C interpreter. You can install it and then, from PHP, send a C program to it :)
$output = `echo -e '#include <stdio.h>\nint main(void) { printf("Hello, World!\\n"); return 0;}' | tcc -run -`;
It's not possible to write C or C++ inside of PHP Code. The only way you can go is writing an extension for PHP. Alternatively you can take a look at HipHop-PHP which transforms any PHP code into highly optimized C++ code (it's developed by Facebook).
Depending on what you want to do, you can just compile your code into an executable file and then start it in php e.g. via the exec-function.
If that is not enough, I am afraid you'll have to look into creating an extension - but that's not as hard as it sounds if you already know c or c++.

Categories