www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 08-09-2006, 08:39 PM
    chestertb's Avatar
    chestertb chestertb is offline
    Privateer and Brigand
     
    Join Date: Aug 2003
    Location: Sydney, Australia
    Posts: 646
    http_post - example needed

    I'm stepping out my comfort zone a bit here...

    I want to use http_post() to post a value to a supplier's server. I've looked at the documentation on php, and am not sure I fully understand it.

    I'm hoping someone here could give me a quick example of how to use http_post to send the following;

    Field name: test
    Value: "Y"

    Field name: ref
    Value :1234546

    Target url: www.testit.com/script.php

    I don't want to draw the form in html and then send it... this is an automated response to my supplier's post to me.

    Thanks
    CTB
    __________________
    Oh Lord, please help me be the person my dog thinks I am.
    Reply With Quote
      #2  
    Old 08-09-2006, 08:54 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    Do you have the PECL HTTP extension installed? If so, you could use the http_post_data() function. If not, you could use the Curl functions, if you have the Curl extension installed.
    __________________
    "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

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #3  
    Old 08-09-2006, 09:11 PM
    chestertb's Avatar
    chestertb chestertb is offline
    Privateer and Brigand
     
    Join Date: Aug 2003
    Location: Sydney, Australia
    Posts: 646
    Doesn't look like pecl is installed.

    http_post_data() is probably the way to go, and I've looked at the php manual but my brain is crying out for an example because I'm not sure how to properly construct

    PHP Code:
    http_post_data("http://www.targeturl.com/script.php", test="Y", ref="123456")
    Or maybe that's all I need and php will do the rest?
    __________________
    Oh Lord, please help me be the person my dog thinks I am.
    Reply With Quote
      #4  
    Old 08-09-2006, 09:30 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    Looks like the http_post_fields() might be better (still requires the PECL HTTP extension, though):
    PHP Code:
    $postData = array
    (
       
    'field1' => 'Value 1',
       
    'field2' => 'Value 2',
       
    'field3' => 'Value 3'
    );
    $result = http_post_fields('http://www.testit.com/script.php', $postData);
    __________________
    "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

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #5  
    Old 08-09-2006, 09:32 PM
    chestertb's Avatar
    chestertb chestertb is offline
    Privateer and Brigand
     
    Join Date: Aug 2003
    Location: Sydney, Australia
    Posts: 646
    Thanks Nogdog. I'll try it.
    And thanks muchly for that other thing too... haven't moved it yet... will get to that later today.
    __________________
    Oh Lord, please help me be the person my dog thinks I am.
    Reply With Quote
      #6  
    Old 08-10-2006, 03:57 AM
    bokeh's Avatar
    bokeh bokeh is offline
    Keep it simple, stupid!
     
    Join Date: Jan 2005
    Location: Alicante (Spain)
    Posts: 7,713
    This isn't perfect:
    PHP Code:
    function HttpRequest( $url, $method = 'GET', $data = NULL, $additional_headers = NULL, $followRedirects = true )
    {
        
    # in compliance with the RFC 2616 post data will not redirected
        
    $method = strtoupper($method);
        
    $url_parsed = @parse_url($url);
        if (!@
    $url_parsed['scheme']) $url_parsed = @parse_url('http://'.$url);
        
    extract($url_parsed);
        if(!
    is_array($data))
        {
            
    $data = NULL;
        }
        else
        {
            
    $ampersand = '';
            
    $temp = NULL;
            foreach(
    $data as $k => $v)
            {
                
    $temp .= $ampersand.urlencode($k).'='.urlencode($v);
                
    $ampersand = '&';
            }
            
    $data = $temp;
        }
        if(!@
    $port) $port = 80;
        if(!@
    $path) $path = '/';
        if((
    $method == 'GET') and ($data)) $query = (@$query)?'&'.$data:'?'.$data;
        if(@
    $query) $path .= '?'.$query;
        
    $out = "$method $path HTTP/1.0\r\n";
        
    $out .= "Host: $host\r\n";
        if(
    $method == 'POST')
        {
            
    $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            
    $out .= "Content-length: " . @strlen($data) . "\r\n";
        }
        
    $out .= (@$additional_headers)?$additional_headers:'';
        
    $out .= "Connection: Close\r\n\r\n";
        if(
    $method == 'POST') $out .= $data."\r\n";
        if(!
    $fp = @fsockopen($host, $port, $es, $en, 5)){
           return
    false;
       }
       
    fwrite($fp, $out);
        while (!
    feof($fp)) {
            
    $s = fgets($fp, 128);
            echo
    $s;
            if (
    $s == "\r\n" ) {
                
    $foundBody = true;
                continue;
            }
            if (
    $foundBody ) {
                
    $body .= $s;
            } else {
                
                
    //echo $s;
                
                
    if(($method != 'POST') and ($followRedirects) and (preg_match('/^Location:(.*)/i', $s, $matches) != false) )
                {
                    
    fclose($fp);
                    return
    HttpRequest( trim($matches[1]) );
                }
                
    $header .= $s;
                if(
    preg_match('@HTTP[/]1[.][01x][\s]{1,}([1-5][01][0-9])[\s].*$@', $s, $matches))
                {
                    
    $status = trim($matches[1]);
                }
            }
        }
        
    fclose($fp);
        return array(
    'head' => trim($header), 'body' => trim($body), 'status' => $status);
    }
    Reply With Quote
      #7  
    Old 08-10-2006, 08:05 AM
    chestertb's Avatar
    chestertb chestertb is offline
    Privateer and Brigand
     
    Join Date: Aug 2003
    Location: Sydney, Australia
    Posts: 646
    wow. I'll need some time to digest all that.
    Thanks for the effort. Very much appreciated.
    __________________
    Oh Lord, please help me be the person my dog thinks I am.
    Reply With Quote
      #8  
    Old 01-24-2007, 02:13 PM
    potherca potherca is offline
    Registered User
     
    Join Date: Jan 2007
    Posts: 2
    Thumbs up thank you bokeh...!

    I ran into the same problem as chestertb and with no PECL or CURL installed I was kinda stuck... so I would like to add a BIG thank you to bokeh, his code worked like a charm.

    I know it's standard knowledge but I though maybe I should list the possible status returns below, for the full text see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    Status Code Definitions
    Informational 1xx
    100 Continue
    101 Switching Protocols
    Successful 2xx
    200 OK
    201 Created
    202 Accepted
    203 Non-Authoritative Information
    204 No Content
    205 Reset Content
    206 Partial Content
    Redirection 3xx
    300 Multiple Choices
    301 Moved Permanently
    302 Found
    303 See Other
    304 Not Modified
    305 Use Proxy
    306 (Unused)
    307 Temporary Redirect
    Client Error 4xx
    400 Bad Request
    401 Unauthorized
    402 Payment Required
    403 Forbidden
    404 Not Found
    405 Method Not Allowed
    406 Not Acceptable
    407 Proxy Authentication Required
    408 Request Timeout
    409 Conflict
    410 Gone
    411 Length Required
    412 Precondition Failed
    413 Request Entity Too Large
    414 Request-URI Too Long
    415 Unsupported Media Type
    416 Requested Range Not Satisfiable
    417 Expectation Failed
    Server Error 5xx
    500 Internal Server Error
    501 Not Implemented
    502 Bad Gateway
    503 Service Unavailable
    504 Gateway Timeout
    505 HTTP Version Not Supported
    hope this helps you other novices out there.
    Reply With Quote
      #9  
    Old 05-29-2007, 02:48 PM
    AndiC AndiC is offline
    Registered User
     
    Join Date: May 2007
    Posts: 1
    Great information thank you

    Is there away to do a "POST" and end up on the response page of the other script.

    for example after you register on one of my sites it takes you to site.com/welcome.htm is it possible to do a http_post from a php script on another site that will still take you to the welcome page on the other site?

    regards

    AC
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:11 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.