Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2

    Post Json Object


    I would list to post the following JSON Object to another page

    PHP Code:
    {
    "username":"test",
    "secret_key":"123",
    "password":"test"

    Can someone please tell me how I could do this? or show me a tutorial for how to post a JSON Object?

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    Stringify your jason object into a hidden field. Then use json_decode on the server side of the resulting $_POST value.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by gw1500se
    Stringify your jason object into a hidden field. Then use json_decode on the server side of the resulting $_POST value.
    Unfortunately I don't know how to do that. I had someone build me a web service that works fine, when an html form posts to it, but when a JSON object posts to it, it doesnt work. I wanted to troubleshoot the issue with posting a JSON Object to it, I just don't know how to do that....
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    It sounds like you need to learn PHP. This is a help forum where you are expected to do the work and we help you over the humps and suggest better techniques. If you need someone to do this for you, then you need to post in the hire a programmer forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    it's also difficult to help without knowing the context.

    Does the data get sent by a user via an HTML form? Or are you talking about an HTTP library like cURL running on the backend? Why do you even want to use JSON?

    Generally, there are many different ways of sending JSON. You can use the "classical" way of embedding it in key-value pairs, you can send it as a multipart request, you can simply output it so that the receiver can fetch it, you can use a full-blown protocol like JSON-RPC etc.

    It all depends on the concrete purpose and needs.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by Jacques1
    Hi,

    it's also difficult to help without knowing the context.

    Does the data get sent by a user via an HTML form? Or are you talking about an HTTP library like cURL running on the backend? Why do you even want to use JSON?

    Generally, there are many different ways of sending JSON. You can use the "classical" way of embedding it in key-value pairs, you can send it as a multipart request, you can simply output it so that the receiver can fetch it, you can use a full-blown protocol like JSON-RPC etc.

    It all depends on the concrete purpose and needs.
    The data is being sent as a JSON Object, Its a username and password with a shared key. Once the web service receives the variables it returns a code of 1 or 0 depending if it's a valid user. This is being used for an iPad app. Ive submitted the variables via html form, and it works fine, its when a JSON object sends info, it doesnt pick up any of the variables.

    This is what the Php Page looks like...
    http://thegameyoujustlostit.com/example.txt
  12. #7
  13. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    HTTP works on strings, so the data you'll post is the plain text

    Code:
    { "username":"test", "secret_key":"123", "password":"test" }
    you can then post that from PHP to some other URI in a number of ways, eg curl or by making the connection directly,

    eg

    PHP Code:
    // Untested, demo process only

    //set some paramters

    $host "example.com";
    $port 80
    $timeout 30;
    $uri "/page.aspx"//yuk
    $req '{ "username":"test", "secret_key":"123", "password":"test" }';
    //or
    //$req = json_encode($some_array_or_object);

    //build the http headers

    $headers.= "POST ".$uri." HTTP/1.0\r\n";
    $headers.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $headers.= "Content-Length: " strlen($req) . "\r\n\r\n";

    //attempt to open a connection
    $fp fsockopen($host,$port,$errno,$errstr,$timeout);

    if(!
    $fp) {

        
    //if the connection attempt failed
        //optionally display the errors or handle in an appropriate mannor
        
    echo $errno,$errstr;
        
    //die();

    } else {
        
    //send the data over the connecction
        
    fputs ($fp$headers.$req);

        
    //optionally read the response
        
    while (!feof($fp)) {
            
    $response.=fgets ($fp1024);
            
        }

        
    //optionally dislplay the response
        
    echo $response//what the remote server (example.com/page.aspx) said


    EDIT

    if you also happen to be receiving such a request, how might you get that data into $_POST?

    Here's how:

    PHP Code:
    $_POST json_decode(file_get_contents("php://input"),true); //true to decode as an array 
    Last edited by Northie; March 5th, 2013 at 10:55 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by Northie
    HTTP works on strings, so the data you'll post is the plain text

    Code:
    { "username":"test", "secret_key":"123", "password":"test" }
    you can then post that from PHP to some other URI in a number of ways, eg curl or by making the connection directly,

    eg

    PHP Code:
    // Untested, demo process only

    //set some paramters

    $host "example.com";
    $post 80
    $timeout 30;
    $uri "/page.aspx"//yuk
    $req '{ "username":"test", "secret_key":"123", "password":"test" }';

    //build the http headers

    $headers.= "POST ".$uri." HTTP/1.0\r\n";
    $headers.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $headers.= "Content-Length: " strlen($req) . "\r\n\r\n";

    //attempt to open a connection
    $fp fsockopen($host,$port,$errno,$errstr,$timeout);

    if(!
    $fp) {

        
    //if the connection attempt failed
        //optionally display the errors or handle in an appropriate mannor
        
    echo $errno,$errstr;
        
    //die();

    } else {
        
    //send the data over the connecction
        
    fputs ($fp$headers.$req);

        
    //optionally read the response
        
    while (!feof($fp)) {
            
    $response.=fgets ($fp1024);
            
        }

        
    //optionally dislplay the response
        
    echo $response//what the remote server (example.com/page.aspx) said


    EDIT

    if you also happen to be receiving such a request, how might you get that info into $_POST?

    Here's how:

    PHP Code:
    $_POST json_decode(file_get_contents("php://input")); 
    Warning: fsockopen() [function.fsockopen]: unable to connect to {mysite.com}:0 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in /home/content/{etc} on line 12
    -1662276Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    There were some typos in my reply, eg $post vs $port.

    Also please note

    Originally Posted by Me
    Untested, demo process only
    This might help in fixing your error
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by Northie
    There were some typos in my reply, eg $post vs $port.

    Also please note



    This might help in fixing your error
    Same issue (the port is right now).

    after reading this It says I need to have some dll's in my apache web server. I am using a hosting enviornment. Is there another to test?
  20. #11
  21. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    you could try using [PHPNET="curl"]curl[/PHPNET]. (Client URL library)

    It's not something I use (I manually make the connections, like above) so I don't have examples, but the docs should help

    Comments on this post

    • Smurff289 agrees : Thank you! (My comments in post below)
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  22. #12
  23. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    Wow, that is some crappy code you had written for you. Hope you didn't pay much for it.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by ptr2void
    Wow, that is some crappy code you had written for you. Hope you didn't pay much for it.
    $100.

    He was awesome, very helpful, and commented all the way through. The code works. So how is it crappy?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    47
    Rep Power
    2
    Originally Posted by Northie
    you could try using [PHPNET="curl"]curl[/PHPNET]. (Client URL library)

    It's not something I use (I manually make the connections, like above) so I don't have examples, but the docs should help
    Great CURL tutorial

    PHP Code:
    $curl curl_init(); 
    curl_setopt_array(
         
    $curl, array( 
         
    CURLOPT_URL => 'mysite.com'
         
    CURLOPT_POST => 1
         
    CURLOPT_POSTFIELDS => array( 
              
    username => 'test'
              
    password => 'test'
              
    secret_key => '123' 
         

    )); 
    $resp curl_exec($curl); 
    curl_close($curl); 
  28. #15
  29. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    Originally Posted by Smurff289
    $100.

    He was awesome, very helpful, and commented all the way through. The code works. So how is it crappy?
    Briefly, it's insecure (regardless of his declaration otherwise), inefficient, and uses the deprecated mysql_* API.

    Does this

    php Code:
    if (phpversion() >= '4.3.0')
      {
      	$string = mysql_escape_string($string);
      }
      else
      {
      	$string = mysql_escape_string($string);
      }

    make any sense whatsoever?

    But that's the typical PHP "developer" response..."it works".
    Last edited by ptr2void; March 5th, 2013 at 03:30 PM.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo