2

cURLを使用せずにHTTP POSTを送信するためにPHP5で動作する次のコードがあります。これを PHP 4.3.0 以降で動作させたい:

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Type: application/json\r\n",
        'content' => $query
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

HTTP コンテキストは PHP5 でのみサポートされています。とにかくこれを PHP 4.3.0 で動作させる方法はありますか? PHP5 または cURL がインストールされていない場合は、フォールバック メソッドが必要です。

4

2 に答える 2

0

誰かがこれで助けを必要とするならば、以下のような何かがトリックをするでしょう。

        $headers = "POST $url 1.1\r\n"; 
        $headers .= "Content-type: application/x-www-form-urlencoded\r\n";
        $headers .= "Content-Type: application/json\r\n";
        $headers .= "Accept: application/json\r\n";
        if (!empty($body)) {
            $headers .= "Content-length: " . strlen($body) . "\r\n";
        }
        $headers .= "\r\n";

        $body = $query;

        if ($fp = fsockopen($host, 80, $errno, $errstr, 180)) {
            fwrite($fp, $headers . $body, strlen($headers . $body));
            fclose();
        }
于 2010-03-19T15:27:30.793 に答える
0

PHP 4 は使用しないでください。廃止されたため、セキュリティ パッチは提供されません。

自分で何かを書いて見たい場合は、fsockopen()から始めてください。必要なことを実行できる基本的な例があり、それを POST 要求に変換するのはそれほど難しくありません。パケット スニファー (Wireshark) を使用して HTTP サンプルを取得し、それを (本質的に) コピーして PHP アプリケーションに貼り付けましたが、仕様と多くのサンプルはオンラインで入手できます。

于 2010-03-19T15:09:38.337 に答える