Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Web Service Returns Nothing

    I'm using:xampp on a windows 7 box and
    http://davidwalsh.name/web-service-php-mysql-xml-json
    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?

    Code:
    <script type="text/javascript" language="javascript">
       $(document).ready(function() {
          $("#driver").click(function(event){
              //$.getJSON('http://192.168.1.66/question.php?user=1&num=10&format=json', function(item) {
    		  $.getJSON('json2.html', function(item) {
                 $('#stage').html('<p> Name: ' + item.posts[1].post.key + '</p>');
              });
          });
       });
       </script>
    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.

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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(?).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply.

    The line
    Code:
    $.getJSON('http://192.168.1.66/question.php?user=1&num=10&format=json', function(item)
    is to the remote php.

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

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by colmtourque View Post
    Thank you for the reply.

    The line
    Code:
    $.getJSON('http://192.168.1.66/question.php?user=1&num=10&format=json', 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!
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Test the return value from http://192.168.1.66/question.php?user=1&num=10&format=json separately.

    .. and (an obvious question) have you changed the database connection from 'localhost'..?
    Last edited by AndrewGSW; 08-20-2012 at 01:56 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Works perfectly.
    My apache logs show the request.
    192.168.1.67 - - [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:
    Code:
    <xml version="1.0"?>
    <cross-domain-policy>
    <allow-access-from domain="*" />
    </cross-domain-policy>

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    <?xml version="1.0" ?>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does not help,
    may i pm you the public address?

  • #9
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by colmtourque View Post
    Does not help,
    may i pm you the public address?
    You could post the php script question.php.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #10
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    Header set Access-Control-Allow-Origin *
    $dbhost = '127.0.0.1';
    $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 */
    	@mysql_close($link);
    }   
    	   
    ?>
    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.

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If that's your current code then you are still connecting to localhost (I mentioned previously but you may have missed).
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #12
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the php is setup on the same server as the mysql server.
    It pulls the results fine.

  • #13
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #14
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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..?!'
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •