www.webdeveloper.com
Results 1 to 8 of 8

Thread: file_get_contents with GET request only passing first parameter

  1. #1
    Join Date
    Jul 2010
    Posts
    8

    file_get_contents with GET request only passing first parameter

    Hello.

    I have a JavaScript frontend using GET to call a PHP script that accesses a servlet. However, the servlet is only receiving the first parameter of the GET, and all of the others are null. This is my PHP script:

    PHP Code:
    <?php
        $getdata 
    http_build_query($_GET);

        
    $opts = array('http' =>
            array(
                
    'method'  => 'GET',
                
    'header'  => 'Content-type: application/x-www-form-urlencoded',
                
    'content' => $getdata
            
    )
        );

        
    $context  stream_context_create($opts);
        
    $servletURL  'http://localhost:8182';
        
    $result file_get_contents(servletURLfalse$context);
        echo 
    $result;
    ?>
    My servlet is using form processing with getFirstValue("value1"), getFirstValue("value2") ... etc, but all parameters after the first are null. Is there something wrong with my PHP?

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    Have you tried to echo $getdata to see if it contains the right info?
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman

  3. #3
    Join Date
    Jul 2010
    Posts
    8
    The data it contains is this:

    'username=myusername&amp;password=mypassword&amp;company=mycompany'

    I was able to solve my problem by using getFirstValue("username") as usual, then for the rest of the parameters, use getFirstValue("amp;password"), getFirstValue("amp;company").

    Thanks for your help. That certainly fixed my problem. Sure feels like a hack though.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    You should be using "&" rather than "&amp;" for the URL. "&amp;" is an HTML character entity, but you are not outputting HTML, you are transmitting a HTTP request.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2010
    Posts
    8
    I am not doing anything of the sort. The http_build_query() call is putting those amps in, and do not know how to ask it not to.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by DEADFA11 View Post
    I am not doing anything of the sort. The http_build_query() call is putting those amps in, and do not know how to ask it not to.
    Then replace all "&amp;" with "&" after you get the string back...doesn't have to be that difficult.

  7. #7
    Join Date
    Mar 2008
    Posts
    73
    Or you could pass it as a serialize array in one variable and unserialize it.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by PETTE View Post
    Or you could pass it as a serialize array in one variable and unserialize it.
    That is a hack. Best to fix the input on the way out rather than create extra work on both sides of the exchange.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles