예제 #1
0
                     exit;
                     break;
                 } elseif (isset($_FILES['file']['tmp_name'][$key]) && isset($_FILES['file']['name'][$key])) {
                     $uploads[] = array('filepath' => $_FILES['file']['tmp_name'][$key], 'filename' => $_FILES['file']['name'][$key], 'formname' => $_POST['key'][$key]);
                 }
             }
         } else {
             if ('cookie' == $_POST['type'][$key]) {
                 $_POST['val'][$key] = rawurlencode($_POST['val'][$key]);
             }
             eval('$' . $_POST['type'][$key] . 's[$_POST[\'key\'][$key]] = $_POST[\'val\'][$key];');
         }
     }
 }
 if (count($gets) > 0) {
     $serverAdder .= '?' . httpBuildQueryStr($gets, null, '&');
 }
 echo PHP_EOL;
 echo '【Request】' . PHP_EOL;
 echo 'URL: ' . $serverAdder . PHP_EOL;
 echo 'Method: ' . $method . PHP_EOL;
 echo 'Language: ' . $language . PHP_EOL;
 echo 'UserAgent: ' . $userAgent . PHP_EOL;
 echo 'X-Image-Scale: ' . $imageScale . PHP_EOL;
 echo 'Gets: ' . PHP_EOL;
 print_r($gets);
 echo 'Posts: ' . PHP_EOL;
 print_r($posts);
 echo 'Cookies: ' . PHP_EOL;
 print_r($cookies);
 echo 'Uploads: ' . PHP_EOL;
예제 #2
0
/**
 * http通信する
 * ※ついでにstream_contexst対応の未熟なPHP4で実装を簡単にする目的での対応
 * @param string URL文字列
 * @param array stream_content_params形式の、コンテキスト多次元配列
 * @param array ベーシック認証用のIDとPASSの組み合わせ配列
 * @return string 戻り値の文字列
 */
function httpRequest($argURLStr, $argContextArr = array(), $argTimeNum = 120, $argBasicAuthParams = array())
{
    $method = "GET";
    if (isset($argContextArr["method"])) {
        $method = $argContextArr["method"];
    }
    $useragent = null;
    if (isset($argContextArr["user_agent"])) {
        $useragent = $argContextArr["user_agent"];
    }
    $headers = "";
    if (isset($argContextArr["header"])) {
        $headers = $argContextArr["header"];
    }
    $content = "";
    if (isset($argContextArr["content"])) {
        $content = $argContextArr["content"];
    }
    $post = null;
    if (isset($argContextArr["post"])) {
        $post = $argContextArr["post"];
    }
    $id = $argBasicAuthParams;
    /* URLを分解 */
    $URL = parse_url($argURLStr);
    if ("https" != $URL['scheme']) {
        /* クエリー */
        if (isset($URL['query'])) {
            $URL['query'] = "?" . $URL['query'];
        } else {
            $URL['query'] = "";
        }
        /* デフォルトのポートは80 */
        if (!isset($URL['port'])) {
            $URL['port'] = 80;
        }
        /* リクエストライン */
        $request = $method . " " . $URL['path'] . $URL['query'] . " HTTP/1.0\r\n";
        /* リクエストヘッダ */
        $request .= "Host: " . $URL['host'] . "\r\n";
        if (0 == strlen($useragent)) {
            $request .= "User-Agent: PHP/" . phpversion() . "\r\n";
        } else {
            $request .= "User-Agent: " . $useragent . "\r\n";
        }
        /* Basic認証用のヘッダ */
        if (isset($URL['user']) && isset($URL['pass'])) {
            $request .= "Authorization: Basic " . base64_encode($URL['user'] . ":" . $URL['pass']) . "\r\n";
        } else {
            if (isset($id['user']) && isset($id['pass'])) {
                $request .= "Authorization: Basic " . base64_encode($id['user'] . ":" . $id['pass']) . "\r\n";
            }
        }
        /* 追加ヘッダ */
        $request .= $headers . "\r\n";
        /* POSTの時はヘッダを追加して末尾にURLエンコードしたデータを添付 */
        if (strtoupper($method) == "POST") {
            if (0 == strlen($headers)) {
                $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
            }
            if (is_array($post)) {
                $tmpContent = httpBuildQueryStr($post, null, '&');
                if (0 < strlen($tmpContent) && 0 < strlen($content)) {
                    $content .= "&";
                }
                $content .= $tmpContent;
            }
            $request .= "Content-Length: " . strlen($content) . "\r\n";
            if ("" != $useragent) {
                $request .= "User-Agent: " . $useragent . "\r\n";
            }
            $request .= "\r\n";
            $request .= $content;
        } else {
            $request .= "\r\n";
        }
        /* WEBサーバへ接続 */
        $errorNo = null;
        $errorMsg = null;
        $fp = fsockopen($URL['host'], $URL['port'], $errorNo, $errorMsg, $argTimeNum);
        /* 接続に失敗した時の処理 */
        if (!$fp) {
            return false;
        }
        //echo "\r\n<br/>".$request."\r\n<br/>";
        /* 要求データ送信 */
        fputs($fp, $request);
        /* 応答データ受信 */
        $response = "";
        while (!feof($fp)) {
            // 100kバイト単位くらいで受け取る
            $tmpStr = @fgets($fp);
            $response .= $tmpStr;
        }
        /* 接続を終了 */
        fclose($fp);
        /* ヘッダ部分とボディ部分を分離 */
        $DATA = split("\r\n\r\n", $response, 2);
        /* メッセージボディを返却 */
        return $DATA[1];
    } else {
        // httpsの場合の処理
        // 新しい cURL リソースを作成します
        $ch = curl_init($argURLStr);
        if ($ch) {
            if (is_array($post)) {
                $tmpContent = httpBuildQueryStr($post, null, '&');
                if (0 < strlen($tmpContent) && 0 < strlen($content)) {
                    $content .= "&";
                }
                $content .= $tmpContent;
            }
            // URL その他のオプションを適切に設定します
            $options = array(CURLOPT_URL => $argURLStr, CURLOPT_HEADER => TRUE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $content, CURLOPT_SSLVERSION => 3, CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_SSL_VERIFYHOST => FALSE, CURLOPT_USERAGENT => $useragent, CURLOPT_PORT => 443, CURLOPT_TIMEOUT => $argTimeNum, CURLOPT_RETURNTRANSFER => TRUE);
            /* Basic認証突破用 */
            if (isset($URL['user']) && isset($URL['pass'])) {
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                curl_setopt($ch, CURLOPT_USERPWD, $URL['user'] . ":" . $URL['pass']);
            } else {
                if (isset($id['user']) && isset($id['pass'])) {
                    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                    curl_setopt($ch, CURLOPT_USERPWD, $id['user'] . ":" . $id['pass']);
                }
            }
            curl_setopt_array($ch, $options);
            // curl_execは指定回リトライを試みる
            // XXX リトライ回数は指定出来るように後日変更
            $retryCount = 3;
            for ($i = 0; $i <= $retryCount; $i++) {
                $response = curl_exec($ch);
                $curl_errorno = curl_errno($ch);
                if ($response === false && $curl_errorno == "28") {
                    // タイムアウトの場合はリトライしない。
                    // 28は"CURLE_OPERATION_TIMEDOUT"のエラーコード
                    $error_msg = " " . __FUNCTION__ . "のcurl_execでタイムアウトしました。\n";
                    $error_msg .= "curl_errno=[" . curl_errno($ch) . "]\n";
                    $error_msg .= "curl_error=[" . curl_error($ch) . "]\n";
                    $error_msg .= "curl_getinfo結果↓\n";
                    $info = curl_getinfo($ch);
                    foreach ($info as $key => $value) {
                        $error_msg .= " " . $key . "=[" . $value . "]\n";
                    }
                    error_log('[error] ' . $error_msg);
                    curl_close($ch);
                    return false;
                } elseif ($response === false && $i === $retryCount) {
                    // curl_execに指定回失敗したらfalseで返す。
                    $error_msg = " " . __FUNCTION__ . "のcurl_execに失敗しました。\n";
                    $error_msg .= "curl_errno=[" . curl_errno($ch) . "]\n";
                    $error_msg .= "curl_error=[" . curl_error($ch) . "]\n";
                    $error_msg .= "curl_getinfo結果↓\n";
                    $info = curl_getinfo($ch);
                    foreach ($info as $key => $value) {
                        $error_msg .= " " . $key . "=[" . $value . "]\n";
                    }
                    curl_close($ch);
                    error_log('[error] ' . $error_msg);
                    return false;
                } elseif ($response === false && $i < $retryCount) {
                    // 指定回以前までの失敗はリトライ
                    continue;
                } else {
                    // ヘッダー部分とボディーを分離
                    $responseStr = substr($response, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
                    // 正常完。cURL リソースを閉じ、システムリソースを解放します
                    curl_close($ch);
                    return $responseStr;
                }
            }
        } else {
            // init失敗はどうしようもないので処理終了
            $error = getColorSizeTagStart("#FF0000") . "情報が取得できませんでした。再度アクセスしてください。" . getColorSizeTagEnd();
            $error_msg = __FUNCTION__ . "のcurl_initに失敗しました。";
            error_log('[error] ' . $error_msg);
            return false;
        }
    }
}