http://www.oschina.net/code/snippet_127872_6370
[1].[代码] [PHP]代码 跳至 [1] [2]
04 | function bbslogin( $user_login , $password , $host , $port = "80" ) { |
06 | $argv = array ( 'cookie' => array ( 'user_login' => $user_login , 'password' => $password , '_wp_http_referer' => '/bbpress/' , 're' => '' , 'remember' => true)); |
07 | foreach ( $argv [ 'cookie' ] as $key => $value ) { |
08 | $params [] = $key . '=' . $value ; |
10 | $params = implode( '&' , $params ); |
11 | $header = "POST /bbpress/bb-login.php HTTP/1.1\r\n" ; |
12 | $header .= "Host:$host:$port\r\n" ; |
13 | $header .= "Content-Type: application/x-www-form-urlencoded\r\n" ; |
14 | $header .= "Content-Length: " . strlen ( $params ) . "\r\n" ; |
15 | $header .= "Connection: Close\r\n\r\n" ; |
17 | $fp = fsockopen ( $host , $port ); |
22 | if (!( strpos ( $str , "Set-Cookie:" ) === false)) { |
23 | $tmparray = explode ( " " , $str ); |
24 | $cookiearray = explode ( "=" , $tmparray [1]); |
25 | $cookiepaths = explode ( "=" , $tmparray [6]); |
26 | $cookiename = urldecode( $cookiearray [0]); |
27 | $cookievalue = urldecode( substr ( $cookiearray [1], 0, strlen ( $cookiearray [1]) - 1)); |
28 | $cookietime = time() + 3600 * 24 * 7; |
29 | $cookiepath = urldecode( substr ( $cookiepaths [1], 0, strlen ( $cookiepaths [1]) - 1)); |
30 | setcookie( $cookiename , $cookievalue , $cookietime , $cookiepath ); |
[2].[代码] [PHP]代码 跳至 [1] [2]
12 | function request_by_socket( $remote_server , $remote_path , $post_string , $port = 80, $timeout = 30) |
14 | $socket = fsockopen ( $remote_server , $port , $errno , $errstr , $timeout ); |
15 | if (! $socket ) die ( "$errstr($errno)" ); |
17 | fwrite( $socket , "POST $remote_path HTTP/1.0\r\n" ); |
18 | fwrite( $socket , "User-Agent: Socket Example\r\n" ); |
19 | fwrite( $socket , "HOST: $remote_server\r\n" ); |
20 | fwrite( $socket , "Content-type: application/x-www-form-urlencoded\r\n" ); |
21 | fwrite( $socket , "Content-length: " . ( strlen ( $post_string ) + 8) . '\r\n' ); |
22 | fwrite( $socket , "Accept:*/*\r\n" ); |
23 | fwrite( $socket , "\r\n" ); |
24 | fwrite( $socket , "mypost=$post_string\r\n" ); |
25 | fwrite( $socket , "\r\n" ); |
27 | while ( $str = trim( fgets ( $socket , 4096))) { |
31 | while (! feof ( $socket )) { |
32 | $data .= fgets ( $socket , 4096); |
42 | * $post_string = "app=request&version=beta" ; |
43 | * request_by_curl( 'http://facebook.cn/restServer.php' , $post_string ); |
45 | function request_by_curl( $remote_server , $post_string ) |
48 | curl_setopt( $ch , CURLOPT_URL, $remote_server ); |
49 | curl_setopt( $ch , CURLOPT_POSTFIELDS, 'mypost=' . $post_string ); |
50 | curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true); |
51 | curl_setopt( $ch , CURLOPT_USERAGENT, "Jimmy's CURL Example beta" ); |
52 | $data = curl_exec( $ch ); |
64 | function request_by_other( $remote_server , $post_string ) |
69 | 'header' => 'Content-type: application/x-www-form-urlencoded' . |
70 | '\r\n' . 'User-Agent : Jimmy\'s POST Example beta' . |
71 | '\r\n' . 'Content-length:' . strlen ( $post_string ) + 8, |
72 | 'content' => 'mypost=' . $post_string ) |
74 | $stream_context = stream_context_create( $context ); |
75 | $data = file_get_contents ( $remote_server , false, $stream_context ); |