Debugging PHP during a jQuery.getJSON request - php

I'm trying to debug some PHP that is called from a jQuery.getJSON request. Everything works on my local machine and one other server but I am having trouble getting it to work on my new server.
I have boiled it down to the following lines:
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
$url = 'http://findyourmp.parliament.uk/api/search?q='.$_GET['q'].'&f=json';
$feed = file_get_contents($url);
Is there anyway I can see which one of these is failing? How would I output these variables without the PHP files sending a response back to the initial jQuery call?
It's all a bit confusing as it works fine on my local machine and other server..
Thanks

Troubleshooting AJAX PHP scripts can sometimes be a hassle! The easiest way would be to see what the console is showing as RESPONSE HEADERS in the XHR section. That doesn't always apply, however.
One thing I've been doing is writing a small script that I can use independently of the rest of the application. Something like this:
<?
require( ................. ); // make sure you include all files you need for the ajax script
error_reporting(ALL);
ini_set('display_errors',1);
include('my.ajax.script.php'); // the AJAX script in question goes here
?>
Save that on your server as ajxchecker.php and you can call it anytime for a quick response.

Use FireFox. Install the FireBug extension and then install FirePHP.
http://getfirebug.com/
http://www.firephp.org/
You will then be able to fully debug your php code.

Related

file_get_contents on times out when run on same server as target

If I put in my php file:
echo file_get_contents("http://www.example.com/myPage.php");
If I run this script on example.com, the page eventually times out. If I run the same script on a different server, it loads correctly immediately. I checked and allow_url_fopen is 'On' in phpinfo(). Any ideas what this could be? Thanks in advance.
Use this class
for getting things DOM from a URL
Like
$html = file_get_html("http://example.com");
echo $html;
Inspect the webpage with chrome/firebug in the network tab.
see what headers you are sending.
Make a new file_get_contents with the same headers.
See here how to send headers along with file_get_contents
This will be like actually opening the website and thus should work just fine.

Requesting file via PHP fails, but succeeds in Python

The URL in question : http://www.roblox.com/asset/?id=149996624
When accessed in a browser, it will correctly download a file (which is an XML document). I wanted to get the file in php, and simply display its contents on a page.
$contents = file_get_contents("http://www.roblox.com/asset/?id=149996624");
The above is what I've tried using (as far as I know, the page does not expect any headers). I get a 500 HTTP error. However, in Python, the following code works and I receive the file.
r = requests.get("http://www.roblox.com/asset/?id=147781188")
I'm confused as to what the distinction is between how these two requests are sent. I am almost 100% it is not a header problem. I've also tried the cURL library in PHP to no avail. Nothing I've tried in PHP seems to succeed with the URL (with any valid id parameter); but Python is able to bring success nonchalantly.
Any insight as to why this issue may be happening would be great.
EDIT : I have already tried copying Python's headers into my PHP request.
EDIT2 : It also appears that there are two requests happening upon navigating to the link.
Is this on a linux/mac host by chance? If so you could use ngrep to see the differences on the request themselves on the wire. Something like the following should work
ngrep -t '^(GET) ' 'src host 127.0.0.1 and tcp and dst port 80'
EDIT - The problem is that your server is responding with a 302 and the PHP library is not following it automatically. Cheers!

PhpStorm how I debug php file when request is initiated from JS?

I have no issue debugging php files independently but when I want to see the request that server
side(php) get from client side locally, I can't. I try to do it by putting breaking point inside php file, hopefully debugger will stop on break point when I debugging my project using chrome.
My php looks like that:
<?php
$response = "Super" <--this line has a breaking point
echo $response
Client side sending the request to server side looks like that:
function ajaxRequest(url, data, requestMethod)
{
$.ajax({
type: requestMethod,
url: url,
data: {
json: data
},
success: responseHandler
});
}
When I run project in debug I get window in chrome with this url:
http://localhost/Jason/index.html?XDEBUG_SESSION_START=19067
And in my PHPStorm debugger I see waiting for connection with ide key 19067
The chrome is displaying to code as if the request already been sent and response has been received without stopping in php break point.
After you start php debugging, try to right click in the browser window and select Inspect in PhpStorm. This should also activate the JS debugger in storm alongside php debug.
Of course you've installed Chrome extension for PhpStorm:
https://chrome.google.com/webstore/detail/jetbrains-ide-support/hmhgeddbohgjknpmjagkdomcpobmllji
Hope this helps.
[Later edit]
Ah, and deactivate any JavaScript minifying you may have!

can you use curl to post to a local file?

I tried using curl to post to a local file and it fails. Can it be done? my two management systems are on the same server and it seems unnecessary to have it traverse the entire internet system just to go to a file on the same hard drive.
Using localhost didn't do the trick.
I also tried to $_SERVER[DOCUMENT_ROOT].'/dir/to/file.php' using post data. It's for an API that is encrypted, so I'm not sure exactly how it works. It's for a billing system I have and I just realized that it sends data back (API).
It's simply post data and an XML response. I could write an html form tag and input fields and get the same result, but there isn't really anything else to know.
The main question is: Does curl have the ability to post to a local file or not?
it is post data. it's for an API that is encrypted so i'm not sure exactly how it works
Without further details nobody can answer then what you should do.
But if it's indeed a POST receival script on the local server, then you can send a POST request to it using the URL:
$url = "https://$_SERVER[SERVER_NAME]/path/to/api.php";
And then receive its output from the cURL call.
$data = curl($url)->post(1)->postdata(array("billing"=>1234345))
->returntransfer(1)->exec();
// (you would use the cumbersome curl_setopt() calls instead)
So you get a XML or JSON or whatever response.
If they're on the same drive, then use file operations instead:
file_put_contents('/path/to/the/file', $contents);
Using CURL should only be done if you absolutely NEED the http layer to get involved for some reason, or if you're dealing with a remote server. Using HTTP would also mean you need to have the 'target' script be able to handle a file upload plus whatever other data you need to send, and then that script would end up having to do file operations ANYWAYS, so in effect you've gone on a round-the-world flight just so you can move from your living room to the kitchen.
file://locafilespec.ext worked for me. I had 2 files in the same folder on a linux box, in a folder that is not served by my webserver, and I used the file:// wrapper to post to file://test.php and it worked great. it's not pretty, but it'll work for dev until I move it to it's final resting place.
Does curl have the ability to post to a local file or not?
To curl local file, you need to setup HTTP server as file:// won't work, so:
npm install http-server -g
Then run the HTTP server in the folder where is the file:
$ http-server
See: Using node.js as a simple web server.
Then test the curl request from the command-line to localhost like:
curl http://127.0.0.1:8081/file.html
Then you can do the same in PHP.

Using PHP on non-supportive server

I have a rather complicated scenario that I have never really had to deal with before. I am creating a website that will be hosted on a web-server without PHP support. But I need to call a PHP script that returns a Flash Slideshow. Is there any way that I can do this? Here is the bit of PHP code that I need to call to return the Flash Slideshow.
<?php
//include slideshow.php to access the Insert_Slideshow function
include "http://mywebsite/slideshow.php";
//insert the slideshow.swf flash file into the web page
//tell slideshow.swf to get the slideshow's data from sample.php created in the first step
//set the slideshow's width to 320 pixels and the height to 240
echo Insert_Slideshow ( "http://mywebsite/slideshow.swf", "http://mywebsite/sample.php", 600, 500 );
?>
To run PHP on your server (not another server) you will definitely need to install a PHP processor.
However since you have a hard-coded URL in there, it looks as though the PHP code is just some kind of utility function for inserting a flash movie.
Run the PHP code on your local computer (for example) and see what HTML it generates, and if it always generates that same HTML, why not just copy it and use that in your website.
You could have the PHP script execute on a PHP enabled webserver somewhere else and include it in an iframe on the page without PHP support. That would be quite ugly, tho.
Although you obviously can't run PHP on a server that doesn't have it, if your slideshow doesn't change frequently perhaps you could run your PHP script on another machine, capture the output, then upload that to your web host.
This is not possible. If you need to call a PHP script, its obvious that you need PHP installed on the web server.

Categories