View Full Version : PHP Web Service Returns Nothing

08-20-2012, 12:40 AM
I'm using:xampp on a windows 7 box and
To write a simple php web service.

I can successfully pull data in json format and if I copy the json format from a webview to a local file I can view the data and pull it.
On the other hand if I try to pull from a the remote web server I can not pull the data using jquery.

Is there any permission you have to set to allow apache or php to allow access to a web service?

<script type="text/javascript" language="javascript">
$(document).ready(function() {
//$.getJSON('', function(item) {
$.getJSON('json2.html', function(item) {
$('#stage').html('<p> Name: ' + item.posts[1].post.key + '</p>');

So in other words in the above it I comment the local file reference and uncomment the url reference my code fails. Same data in both locations.

Thank you in advance for any help.

08-20-2012, 01:26 AM
Your local request is to a .php file, but remotely is to .html. I would rename json2.html to .php.

You have no querystring attached to 'json2.html' (but you do with the local request) but I assume that you are aware of this..?

BTW language="javascript" has been out-of-favour (deprecated) for a long while now and can be deleted.

I can't see permissions as being an issue, as it's within the same domain(?).

08-20-2012, 01:32 AM
Thank you for the reply.

The line

$.getJSON('', function(item)
is to the remote php.

the html line was to the local file, and as such required no query string.

08-20-2012, 01:44 AM
Thank you for the reply.

The line

$.getJSON('', function(item)
is to the remote php.

the html line was to the local file, and as such required no query string.

Sorry.. it's getting late!

08-20-2012, 01:47 AM
Test the return value from ( separately.

.. and (an obvious question) have you changed the database connection from 'localhost'..? :thumbsup:

08-20-2012, 01:57 AM
Works perfectly.
My apache logs show the request. - - [19/Aug/2012:16:51:26 -0500] "GET /question.php?user=1&num=10&format=json HTTP/1.1" 200 969 "-" "Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20100101 Firefox/14.0.1"

That is what I copied and pasted into json2.html and when I query that I can show results.

Can it be a crossdomain issue?
I created a crossdomain.xml in the root of the htdocs folder and added:

<xml version="1.0"?>
<allow-access-from domain="*" />

08-20-2012, 02:02 AM
<?xml version="1.0" ?>

08-20-2012, 02:10 AM
Does not help,
may i pm you the public address?

08-20-2012, 02:16 AM
Does not help,
may i pm you the public address?

You could post the php script question.php.

08-20-2012, 02:21 AM
Header set Access-Control-Allow-Origin *
$dbhost = '';
$dbuser = '';
$dbpass = '';
$dbname = '';

/* require the user as the parameter */
if(isset($_GET['user']) && intval($_GET['user'])) {

/* soak in the passed variable or set our own */
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
$user_id = intval($_GET['user']); //no default

/* connect to the db */
$link = mysql_connect('localhost',$dbuser ,$dbpass) or die('Cannot connect to the DB');
mysql_select_db($dbname,$link) or die('Cannot select the DB');

/* grab the posts from the db */
$query = "SELECT * " .
"FROM table";
$result = mysql_query($query,$link) or die('Errant query: '.$query);

/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);

/* output in necessary format */
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
else {
header('Content-type: text/xml');
echo '<posts>';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
echo '</',$key,'>';
echo '</posts>';

/* disconnect from the db */


If I browse the link it shows perfectly formatted json that I can then take to the the local machine and query easily.
Sorry, I've used json on twitter in the past, but I've never built my own service. Thank you for all of your help so far.

08-20-2012, 02:26 AM
If that's your current code then you are still connecting to localhost (I mentioned previously but you may have missed).

08-20-2012, 02:52 AM
the php is setup on the same server as the mysql server.
It pulls the results fine.

08-20-2012, 03:14 AM
And I believe it must have been the badly formed php

header("Access-Control-Allow-Origin: *");

This appears to have fixed it because it is running now.

Sweet, very sweet.

Does that actually make sense though.
If so I need to look that line up and see how I restrict it properly.

08-20-2012, 02:32 PM
Does that actually make sense though.

Dunno, but glad you got it working.

BTW It would be worth considering that, instead of dying, your json page return an empty object. Or, an object with a single key/value of 'errormsg', 'Doh! SQL no workee..?!'