banner

[Rule] Rules  [Home] Main Forum  [Portal] Portal  
[Members] Member Listing  [Statistics] Statistics  [Search] Search  [Reading Room] Reading Room 
[Register] Register  
[Login] Loginhttp  | https  ]
 
Forum Index Thảo luận hệ điều hành Windows KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)?  XML
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 30/09/2006 01:44:53 (+0700) | #1 | 26593
[Avatar]
phamquoc_truong
Elite Member

[Minus]    0    [Plus]
Joined: 04/04/2004 07:54:12
Messages: 79
Location: PeaceWorld
Offline
[Profile] [PM]
Mình muốn kết nối đến 1 trang sử dụng phương thức post tất nhiên nếu là dùng form thì không có gì để nó cả.
Ví dụ : file test.php nhận 2 biến theo method = post là var1 và var2. Thông thường thì sử dụng form, method=post có :
name=var1 value='xxx' và name=var2 value='xxx2'
khi submit test.php sẽ hiển thị :
biến 1 là : xxx
biến 2 là : xxx2
Giờ mình muốn tạo 1 trang : post_test.php khi vào trang này tự động sẽ kết nối đến trang test.php với 2 biến trên theo method là post và hiển thị những gì nhận được tại trang test.php

Mình phải sử dụng lệnh nào trong php ? Các biến var1=xxx, var2=xxx2 được truyền như thế nào ?

Mong các bạn giúp đỡ !!
[Up] [Print Copy]
  [Question]   Re: KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 30/09/2006 02:20:10 (+0700) | #2 | 26601
[Avatar]
Z0rr0
Q+WRtaW5pc3RyYXRvc+g

Joined: 14/08/2002 12:52:01
Messages: 1323
Location: Underground
Offline
[Profile] [PM] [WWW] [Yahoo!]
Có 2 cách thực hiện việc tạo 1 POST request trong PHP:
- Sử dụng phương thức http_post_data http://us3.php.net/manual/en/function.http-post-data.php hoặc http_post_fields: http://us3.php.net/manual/en/function.http-post-fields.php
Cách này cần cài đặt PECL extension http://pecl.php.net/package/pecl_http
Ví dụ:
Code:
$postFields = array
(
   'var1' => 'xxx',
   'var2' => 'xxx'
);
$result = http_post_fields('http://domain.com/test.php', $postFields);

- Tự tạo request bằng cách thiết lập socket và tạo HTTP message với các thông số kèm theo. Bạn xem thêm về socket connection tại www.php.net
Hibernating
[Up] [Print Copy]
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 30/09/2006 03:37:45 (+0700) | #3 | 26618
[Avatar]
phamquoc_truong
Elite Member

[Minus]    0    [Plus]
Joined: 04/04/2004 07:54:12
Messages: 79
Location: PeaceWorld
Offline
[Profile] [PM]
Cảm ơn Z0rr0!
- Cách thứ nhất của bạn thì bắt buộc phải cài thêm PECL extension <== không thể ứng dụng được trên share host
- Mình đã đọc đi đọc lại phần socket trên php.net rất nhiều lần nhưng vẫn không tìm được cái mình cần nên mình mới post bài lên.

Z0rr0 và mọi người ai có thể làm ở phần socket hay có cách khác thì chỉ bảo cho mình nhé.

Thanks !
[Up] [Print Copy]
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 30/09/2006 09:28:23 (+0700) | #4 | 26689
[Avatar]
Z0rr0
Q+WRtaW5pc3RyYXRvc+g

Joined: 14/08/2002 12:52:01
Messages: 1323
Location: Underground
Offline
[Profile] [PM] [WWW] [Yahoo!]
Có ngay một ví dụ mới search được trên net, bạn thử xem nhé:
Code:
function HttpRequest( $url, $method = 'GET', $data = NULL, $additional_headers = NULL, $followRedirects = true )
{
    # in compliance with the RFC 2616 post data will not wwwected
    $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);
}


Khi gọi hàm truyền tham số $method là 'POST'.
Hibernating
[Up] [Print Copy]
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 01/10/2006 01:09:39 (+0700) | #5 | 26832
[Avatar]
SuperChicken
Elite Member

[Minus]    0    [Plus]
Joined: 11/07/2006 18:31:27
Messages: 635
Location: bottom of hell
Offline
[Profile] [PM]
Trùi, hồi đó tui kiếm cái khỉ này wá chừng, rốt cuộc phải down 1 cái class bên PEAR. Cái ví dụ mà Z0rr0 đưa cũng send được đấy, có điều nó xuất hết ra màn hình nguyên cả cái Response, từ header cho đến body, mặc dù tui đã gán giá trị trả về vào 1 biến và chưa đụng gì đến biến đó cả, có cách nào khắc phục cái đó ko?
@phamquoc_truong: bạn có cần cái class của tui không, xài tốt lắm, nếu cần thì tui send cho.
*PS: wên mất, tui thấy trong cái đống extension có 1 cái php_http.dll, ko biết xài thế nào nhỉ, có ai biết phải tìm doc của các extension đó ở đâu ko?
[Up] [Print Copy]
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 01/10/2006 05:43:54 (+0700) | #6 | 26904
[Avatar]
phamquoc_truong
Elite Member

[Minus]    0    [Plus]
Joined: 04/04/2004 07:54:12
Messages: 79
Location: PeaceWorld
Offline
[Profile] [PM]
hì ! Chắc Z0rr0 lấy một phần mã nguồn ở đâu đó, mình đã xem kĩ lại phần socket connection và kết hợp với php manual kết quả thật là mĩ mãn, không còn gì để nói . . .
Cảm ơn Z0rr0.
@Siêu trộm : Thanks !
Bạn post lên để mọi người cùng biết ^_^

HAVE FUN !
[Up] [Print Copy]
  [Question]   KẾT NỐI ĐẾN 1 TRANG SỬ DỤNG METHOD POST (không sử dụng form)? 01/10/2006 05:55:43 (+0700) | #7 | 26912
[Avatar]
SuperChicken
Elite Member

[Minus]    0    [Plus]
Joined: 11/07/2006 18:31:27
Messages: 635
Location: bottom of hell
Offline
[Profile] [PM]
Éc éc, hồi nãy test thử, cop nguyên xi đâu có thèm đọc source smilie) , vừa nhìn lại thì thấy cái lệnh echo lù lù 1 đống smilie)
@phamquoc_truong: http://download.yousendit.com/F358FFE22E81BE07 (nó có đến mấy class, nhiều lúc include vô thì thấy nó phí phí thế nào í smilie)
[Up] [Print Copy]
[digg] [delicious] [google] [yahoo] [technorati] [reddit] [stumbleupon]
Go to: 
 Users currently in here 
1 Anonymous

Powered by JForum - Extended by HVAOnline
 hvaonline.net  |  hvaforum.net  |  hvazone.net  |  hvanews.net  |  vnhacker.org
1999 - 2013 © v2012|0504|218|