debug php code of a facebook app present on heroku - php

At first apologies in advance if there is some problem in my ques, i am new to heroku ,my basic problem is i have some messed up code, where i want to test if i am able to fetch facebook variables in my own code and use them..
in my .php file i want to put name for person using my facebook app to displayname array variable of array and url of array should get the application users picture..i took the idea to assign these value via the index.php file provided by facebook itself.
My .php file code is :-
$basic = $facebook->api('/me');
$options = array(
'displayName' => he(idx($basic, 'name')),
'image' => array(
'url' => 'https://graph.facebook.com/'.he($id).'/picture?type=square',
'height => '48',
'width => '48'
)
);
but there is something wrong going here which i cant figure out.
i tried to debug it via javascript or other techniques but now able to connect the .php file to some .js file by any means to transfer variable values present in .php file and print them on my browser,i use to edit code at my own system and push it via git and since the code is executed at heroku i cant figure out what errors are creping in..i am using free account as per now so is there any way i can see my code in execution at heroku.. or any help to debug my code efficiently..
Edit1: alternatively is there any way i can pass these variable from my .PHP file to a separate .JS file and print variables in message box or something..any example code given will help a lot..there are many questions asked in this regard to transfer variables from separate .PHP file to separate .JS file..but i found no direct answer for it, all suggest workarounds but no direct way... questions i visited for it are ..
What's the best way to pass a PHP variable to Javascript?
Grab/input php variable in javascript?
and some more but dint find the perfect answer.
Edit2: if my ques needs more info plz let me know,and if second option is the choice left to debug my code ..then can someone give me an example with transferring variable/array present in testfile.php file say present at appfolder/php/lib/testfile.php and output it on browser in HTML format using testjs.js file say present at appfolder/lib/js/testjs.js

A common exchange format between PHP (or other language) and JavaScript is JSON. You can encode an array (or an php object) to json using json_encode, in PHP. Like this :
$options_json = json_encode($options);
So, you can write this javascript variable in your html results, like this :
echo '<script>var options = ', json_encode($options), ';</script>';
Your picture will then be accessible using javascript :
console.log(options);
console.log(options.url);

Related

Any way to get source code in actual php format from iframe

I am working with iframe in my php project ,and use iframe to load another php project also from htdocs(local storage).
and i use this javascript to get source code of single page of project from iframe window
var code=document.getElementById("myframe").contentWindow.document.documentElement.outerHTML;
and i get the code but all the php statements are in simplified format that is, for an example
<?php echo "hi"> is changed to hi
and my question is that there is any way to get in actual php format like this
<?php echo "hi"> into code variable???
If you want to get the php-sourcecode you will have to write a script for that.
Example (reader.php):
<?php
echo file_get_contents('example.php'); // or $_GET['file'] insetad of 'example.php'
The iframe would have to call reader.php?file=example.php then.
Be warned
This is highly insecure. You will have to take care of thw following things:
Sanitizing user-input
Prevent directory traversal
Limit access to only the files you want to be readable
Be very very sure there are no sensitive information in your php-files.

Azure Functions with PHP

I'm trying out Azure Functions using PHP.
Getting the request information is not working for me.
I've not been able to find any documentation at all with the information of how to use Azure Functions with PHP code.
According to the only couple of examples, it seems that in order to retrieve the input information you need to first get the content of the req variable (or whatever name you assign in the function configuration).
That has the path of the file containing the request information (in theory).
$input_path = getenv('req');
So far, if I check the content of it, I get something like this:
D:\local\Temp\Functions\Binding\e2b6e195-02f7-481b-a279-eef6f82bc7b4\req
If I check if the file exists it says true, but the file size is 0.
Do anyone knows what to do here? Anyone with an example? Does anyone know where the documentation is?
Thanks
Ok, unfortunately there's pretty limited documentation out there for php as you have discovered.
At present, looking at the code might be the best doc. Here is the InitializeHttpRequestEnvironmentVariables function that adds request metadata to the environment for the script languages (node, powershell, php, python).
Important environment variables are:
REQ_ORIGINAL_URL
REQ_METHOD
REQ_QUERY
REQ_QUERY_<queryname>
REQ_HEADERS_<headername>
REQ_PARAMS_<paramname>
I'm assuming you've made a GET request, in which case there is no content (req is an empty file), but you will see that these other environment variables contain request data. If you were to make a POST request with a body then req would have data.
here is a full example parsing a GET request in PHP with an Azure Function :)
https://www.lieben.nu/liebensraum/2017/08/parsing-a-get-request-in-php-with-an-azure-function/
snippet from source:
<?php
//retrieve original GET string
$getReqString = getenv('REQ_QUERY');
//remove the ? for the parse_str function
$getReqString = substr($getReqString,1,strlen($getReqString));
//convert the GET string to an array
$parsedRequest = array();
parse_str($getReqString,$parsedRequest);
//show contents of the new array
print_r($parsedRequest);
//show the value of a GET variable
echo $parsedRequest["code"];
?>

How to write response to file using php

I wish to write the response of hitting a given url into the href attribute of an anchor tag using PHP. How can I do this?
Here's an example of what I excpect to happen
mylink.com/getdoc?name=documentA
returns a string as a response:
mylink.com/document2012-03-15.pdf
I need to write this response (using PHP into the href attribute as shown below:
Open Document A
(so the above will be the final source of my page.
I think there are a few ways to do what you want. Not all of them will work exactly as you ask for, but the end result should be the same.
Solution one
My first possible solution was already posted by #shanethehat. You could use file_get_contents to call your PHP script via HTTP and get the response.
Solution two
Another possible solution was suggested in the comments of the post by #YourCommonSense. You could simply include the getdoc script in the PHP script that is generating your HTML file, like this:
$_GET["name"] = "documentA";
echo " Open Document A ";
Solution three
Or you could change the way the getdoc script works. You could use a script more like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment; filename=\"{$_GET["name"]}\"");
readfile($_GET["name"]);
And you keep your link like this: Open Document A . When getdoc.php is called, it will get the specified file and start a file download.
NOTE: you should probably do some input sanitization with this method (removing slashes, making sure the file ends in .pdf, etc) to make sure someone doesn't try to get a file they're not allowed to get.
That's all I'm coming up with at the moment. There might be a more clever way to do it, but hopefully one of these solutions will do it for you. I would try solution 2 or 3 first, and if they don't work out for you, then go with solution 1.
<?php
//get output from URL
$myfile = file_get_contents('http://mylink.com/getdoc?name=documentA');
?>
Open Document A
How to write response to file using php
Noway.
PHP do not process HTTP requests.
You have to set up your web server to do the rewrite.
There are 100500 questions under mod_rewrite tag, you will find the solution easily.
Note that you may wish to rewrite your url to /getdoc.php?name=document2012-03-15.pdf, not one you mentioned in your question

execute some function in some php file on another server(apache) and get the returned value

I want to access some function written in some php file on another server and receive the input, I have access to those server files so that I can change them for proper configuration, I have all the privilleges to do so, can anybody please guide me how can I do it, thank you
$result = file_get_contents("http://some.server/out.php");
and in this out.php
<?php
include 'some.php';
function();
I think you can implement web service or an api, and the output could be in xml or json read the content and use it on your website.
It is just Facebook graph API using URL
https://graph.facebook.com/Pepsi
The above link will fetch information regarding Facebook page of Pepsi.

Why is javascript not able to use a javascript variable I declared in a php file?

Hey everybody, this issue has had me stumped for the last week or so, here's the situation:
I've got a site hosted using GoDaddy hosting. The three files used in this issue are index.html , milktruck.js , and xml_http_request.php all hosted in the same directory.
The index.html file makes reference to the milktruck.js file with the following code:
<script type="text/javascript" src="milktruck.js"></script>
The milktruck.js file automatically fires when the site is opened. The xml_http_request.php has not fired at this point.
On line 79 out of 2000 I'm passing the variable "simple" to a function within the milktruck.js file with:
placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);
"simple" was never initialized within the milktruck.js file. Instead I've included the following line of code in the xml_http_request.php file:
echo "<script> var simple = 'string o text'; </script>";
At this point I have not made any reference whatsoever to the xml_http_request.php file within the milktruck.js file. I don't reference that file until line 661 of the milktruck.js file with the following line of code:
xmlhttp.open('GET',"xml_http_request.php?pid="+pid+"&unLoader=true", false);
Everything compiles (I'm assuming because my game runs) , however the placem function doesn't run properly because the string 'string o text' never shows up.
If I was to comment out the line of code within the php file initializing "simple" and include the following line of code just before I call the function placem, everything works fine and the text shows up:
var simple = 'string o text';
Where do you think the problem is here? Do I need to call the php file before I try using the "simple" variable in the javascript file? How would I do that? Or is there something wrong with my code?
So, we meet again!
Buried in the question comments is the link to the actual Javascript file. It's 2,200 lines, 73kb, and poorly formatted. It's also derived from a demo for the Google Earth API.
As noted in both the comments here and in previous questions, you may be suffering from a fundamental misunderstanding about how PHP works, and how PHP interacts with Javascript.
Let's take a look at lines 62-67 of milktruck.js:
//experiment with php and javascript interaction
//'<?php $simpleString = "i hope this works"; ?>'
//var simple = "<?php echo $simpleString; ?>";
The reason this never worked is because files with the .js extension are not processed by PHP without doing some bizarre configuration changes on your server. Being on shared hosting, you won't be able to do that. Instead, you can rename the file with the .php extension. This will allow PHP to process the file, and allow the commands you entered to actually work.
You will need to make one more change to the file. At the very top, the very very top, before anything else, you will need the following line:
<?php header('Content-Type: text/javascript'); ?>
This command will tell the browser that the file being returned is Javascript. This is needed because PHP normally outputs HTML, not Javascript. Some browsers will not recognize the script if it isn't identified as Javascript.
Now that we've got that out of the way...
Instead I've included the following line of code in the xml_http_request.php file: <a script tag>
This is very unlikely to work. If it does work, it's probably by accident. We're not dealing with a normal ajax library here. We're dealing with some wacky thing created by the Google Earth folks a very, very long time ago.
Except for one or two in that entire monolithic chunk of code, there are no ajax requests that actually process the result. This means that it's unlikely that the script tag could be processed. Further, the one or two that do process the result actually treat it as XML and return a document. It's very unlikely that the script tag is processed there either.
This is going to explain why the variable never shows up reliably in Javascript.
If you need to return executable code from your ajax calls, and do so reliably, you'll want to adopt a mature, well-tested Javascript library like jQuery. Don't worry, you can mix and match the existing code and jQuery if you really wanted to. There's an API call just to load additional scripts. If you just wanted to return data, that's what JSON is for. You can have PHP code emit JSON and have jQuery fetch it. That's a heck of a lot faster, easier, and more convenient than your current unfortunate mess.
Oh, and get Firebug or use Chrome / Safari's dev tools, they will save you a great deal of Javascript pain.
However...
I'm going to be very frank here. This is bad code. This is horrible code. It's poorly formatted, the commenting is a joke, and there are roughly one point seven billion global variables. The code scares me. It scares me deeply. I would be hesitant to touch it with a ten foot pole.
I would not wish maintenance of this code on my worst enemy, and here you are, trying to do something odd with it.
I heartily encourage you to hone your skills on a codebase that is less archaic and obtuse than this one before returning to this project. Save your sanity, get out while you still can!
perhaps init your values like this:
window.simple = 'blah blah blah'
then pass window.simple
You could try the debugger to see what is going on, eg. FireBug

Categories