/** * HTTPリクエストをサーバに送信して、レスポンス(WapResponseオブジェクト)を取得する * * @thanks http://www.spencernetwork.org/memo/tips-3.php * * @param WapRequest $req * @param array $options * @return WapResponse */ public function request(WapRequest $req, array $options = array()) { if (!empty($options['onlyHeader'])) { $req->setOnlyHeader($options['onlyHeader']); } if (!($purl = parse_url($req->url))) { $res = new WapResponse(); $res->message = 'parse_url() failed'; return $res; } if (isset($purl['query'])) { $purl['query'] = '?' . $purl['query']; } else { $purl['query'] = ''; } $default_port = $purl['scheme'] == 'https' ? 443 : 80; // プロキシ if ($req->proxy) { $send_host = $req->proxy['host']; $send_port = isset($req->proxy['port']) ? $req->proxy['port'] : $default_port; $send_path = $req->url; } else { $send_host = $purl['host']; $send_port = isset($purl['port']) ? $purl['port'] : $default_port; $send_path = $purl['path'] . $purl['query']; } // SSL if ($purl['scheme'] == 'https') { $send_host = 'ssl://' . $send_host; } $request = $req->method . ' ' . $send_path . ' HTTP/1.0' . self::CRLF; $request .= 'Host: ' . $purl['host'] . self::CRLF; if ($this->_agent) { $request .= 'User-Agent: ' . $this->_agent . self::CRLF; } $request .= 'Connection: Close' . self::CRLF; //$request .= 'Accept-Encoding: gzip' . self::CRLF; if ($req->modified) { $request .= 'If-Modified-Since: ' . $req->modified . self::CRLF; } // Basic認証用のヘッダ if (isset($purl['user']) && isset($purl['pass'])) { $request .= 'Authorization: Basic ' . base64_encode($purl['user'] . ':' . $purl['pass']) . self::CRLF; } // 追加ヘッダ if ($req->headers) { $request .= $req->headers; } // POSTの時はヘッダを追加して末尾にURLエンコードしたデータを添付 if (strtoupper($req->method) == 'POST') { // 通常はURLエンコードする if (empty($req->noUrlencodePost)) { foreach ($req->post as $name => $value) { $POST[] = $name . '=' . rawurlencode($value); } $postdata_content_type = 'application/x-www-form-urlencoded'; // ●ログインのときなどはURLエンコードしない } else { foreach ($req->post as $name => $value) { $POST[] = $name . '=' . $value; } $postdata_content_type = 'text/plain'; } $postdata = implode('&', $POST); $request .= 'Content-Type: ' . $postdata_content_type . self::CRLF; $request .= 'Content-Length: ' . strlen($postdata) . self::CRLF; $request .= self::CRLF; $request .= $postdata; } else { $request .= self::CRLF; } $res = new WapResponse(); // WEBサーバへ接続 if ($this->_timeout > 0) { if ($this->_atFsockopen) { $fp = @fsockopen($send_host, $send_port, $errno, $errstr, $this->_timeout); } else { $fp = fsockopen($send_host, $send_port, $errno, $errstr, $this->_timeout); } } else { if ($this->_atFsockopen) { $fp = @fsockopen($send_host, $send_port, $errno, $errstr); } else { $fp = fsockopen($send_host, $send_port, $errno, $errstr); } } if (!$fp) { $res->code = $errno; // ex) 602 $res->message = $errstr; // ex) "Connection Failed" return $res; } if ($this->_readTimeout > 0) { stream_set_timeout($fp, $this->_readTimeout, 0); } fputs($fp, $request); $body = ''; // header response while (!p2_stream_eof($fp, $timed_out)) { $l = fgets($fp, 128000); //echo $l."<br>"; // // ex) HTTP/1.1 304 Not Modified if (preg_match('/^(.+?): (.+)\\r\\n/', $l, $matches)) { $res->headers[$matches[1]] = $matches[2]; } elseif (preg_match('/HTTP\\/1\\.\\d (\\d+) (.+)\\r\\n/', $l, $matches)) { $res->code = (int) $matches[1]; $res->message = $matches[2]; $res->headers['HTTP'] = rtrim($l); } elseif ($l == self::CRLF) { break; } } // body response if (!$req->onlyHeader) { while (!p2_stream_eof($fp, $timed_out)) { $body .= fread($fp, 4096); } $res->setContent($body); } fclose($fp); // リダイレクト(301 Moved, 302 Found)を追跡 // RFC2616 - Section 10.3 /*if ($GLOBALS['trace_http_redirect']) { if ($res->code == 301 || ($res->code == 302 && $req->isSafeMethod())) { if (!$this->_redirectCache) { $this->_maxRedirect = 5; $this->_redirectCount = 0; $this->_redirectCache = array(); } while ($res->isRedirect() && isset($res->headers['Location']) && $this->_redirectCount < $this->_maxRedirect) { $this->_redirectCache[] = $res; $req->setUrl($res->headers['Location']); $res = $this->request($req); $this->_redirectCount++; } } } elseif ($res->isRedirect() && isset($res->headers['Location'])) { $res->message .= " (Location: <a href=\"{$res->headers['Location']}\">{$res->headers['Location']}</a>)"; }*/ return $res; }
/** * HTTPリクエストをサーバに送信して、レスポンス(WapResponseオブジェクト)を取得する * * @thanks http://www.spencernetwork.org/memo/tips-3.php * * @access public * @param array $options * @return WapResponse */ function request($req, $options = array()) { if (!empty($options['onlyHeader'])) { $req->setOnlyHeader($options['onlyHeader']); } if (!($purl = parse_url($req->url))) { $res = new WapResponse(); //$res->code = $res->message = "parse_url() failed"; return $res; } if (isset($purl['query'])) { $purl['query'] = "?" . $purl['query']; } else { $purl['query'] = ''; } $default_port = $purl['scheme'] == 'https' ? 443 : 80; // プロキシ if ($req->proxy) { $send_host = $req->proxy['host']; $send_port = isset($req->proxy['port']) ? $req->proxy['port'] : $default_port; $send_path = $req->url; } else { $send_host = $purl['host']; $send_port = isset($purl['port']) ? $purl['port'] : $default_port; $send_path = $purl['path'] . $purl['query']; } // SSL if ($purl['scheme'] == 'https') { $send_host = 'ssl://' . $send_host; } $request = $req->method . " " . $send_path . " HTTP/1.0\r\n"; $request .= "Host: " . $purl['host'] . "\r\n"; if ($this->agent) { $request .= "User-Agent: " . $this->agent . "\r\n"; } $request .= "Connection: Close\r\n"; //$request .= "Accept-Encoding: gzip\r\n"; if ($req->modified) { $request .= "If-Modified-Since: {$req->modified}\r\n"; } // Basic認証用のヘッダ if (isset($purl['user']) && isset($purl['pass'])) { $request .= "Authorization: Basic " . base64_encode($purl['user'] . ":" . $purl['pass']) . "\r\n"; } // 追加ヘッダ if ($req->headers) { $request .= $req->headers; } // POSTの時はヘッダを追加して末尾にURLエンコードしたデータを添付 if (strtoupper($req->method) == 'POST') { // 通常はURLエンコードする if (empty($req->noUrlencodePost)) { while (list($name, $value) = each($req->post)) { $POST[] = $name . '=' . urlencode($value); } $postdata_content_type = 'application/x-www-form-urlencoded'; // ●ログインのときなどはURLエンコードしない } else { while (list($name, $value) = each($req->post)) { $POST[] = $name . '=' . $value; } $postdata_content_type = 'text/plain'; } $postdata = implode('&', $POST); $request .= 'Content-Type: ' . $postdata_content_type . "\r\n"; $request .= 'Content-Length: ' . strlen($postdata) . "\r\n"; $request .= "\r\n"; $request .= $postdata; } else { $request .= "\r\n"; } $res = new WapResponse(); // WEBサーバへ接続 if ($this->timeout) { if ($this->atFsockopen) { $fp = @fsockopen($send_host, $send_port, $errno, $errstr, $this->timeout); } else { $fp = fsockopen($send_host, $send_port, $errno, $errstr, $this->timeout); } } else { if ($this->atFsockopen) { $fp = @fsockopen($send_host, $send_port, $errno, $errstr); } else { $fp = fsockopen($send_host, $send_port, $errno, $errstr); } } if (!$fp) { $res->code = $errno; // ex) 602 $res->message = $errstr; // ex) "Connection Failed" return $res; } fputs($fp, $request); // header response while (!feof($fp)) { $l = fgets($fp, 8192); // ex) HTTP/1.1 304 Not Modified if (preg_match('/^(.+?): (.+)\\r\\n/', $l, $matches)) { $res->headers[$matches[1]] = $matches[2]; } elseif (preg_match("/HTTP\\/1\\.\\d (\\d+) (.+)\r\n/", $l, $matches)) { $res->code = $matches[1]; $res->message = $matches[2]; $res->headers['HTTP'] = rtrim($l); } elseif ($l == "\r\n") { break; } } // body response if (!$req->onlyHeader) { $body = ''; while (!feof($fp)) { $body .= fread($fp, 8192); } $res->setContent($body); } fclose($fp); // リダイレクト(301 Moved, 302 Found)を追跡 // RFC2616 - Section 10.3 /*if ($GLOBALS['trace_http_redirect']) { if ($res->code == '301' || ($res->code == '302' && $req->is_safe_method())) { if (empty($this->redirectCache)) { $this->maxRedirect = 5; $this->redirectCount = 0; $this->redirectCache = array(); } while ($res->is_redirect() && isset($res->headers['Location']) && $this->redirectCount < $this->maxRedirect) { $this->redirectCache[] = $res; $req->setUrl($res->headers['Location']); $res = $this->request($req); $this->redirectCount++; } } } elseif ($res->is_redirect() && isset($res->headers['Location'])) { $res->message .= " (Location: <a href=\"{$res->headers['Location']}\">{$res->headers['Location']}</a>)"; }*/ return $res; }