2017-02-09 6 views
0

でPHPのフォームを送信します。は、私は次の要求を建てCPPを使用して手動でPOSTリクエストを構築し、それ

私はOK messegeをOKにしますが、 "echo"(print)コマンドを使わないと、私はPHP側で行いました。

だから、たとえば、通常のクロムキャプチャ応答は次のようになります。

Hello michaeliko\n 
    \n 
    \n 
    \n 
    <form method="post">\n 
    \n 
     Subject: <input type="text" name="command"> <br>\n 
     \n 
    </form>\t 

そして、私はabouve示したPOSTリクエストのために、私はウェブサイトのサーバーからこれを取得します:

\n 
    \n 
    \n 
    <form method="post">\n 
    \n 
     Subject: <input type="text" name="command"> <br>\n 
     \n 
    </form>\t 

PHP側は次のようになります:

<?php 

if( isset($_POST['command'])) 
{ 
    $command = $_POST['command']; 
    echo 'Hello ' . $command . "\n"; 
} 

?> 



<form method="post"> 

    Subject: <input type="text" name="command"> <br> 

</form> 

私はこのコードを多くの方法で操作しようとしましたが、無回答を見つける。 私のリクエストに間違いがありますか?

答えて

1

私はまだコメントできませんので、次のような質問はできません:どのようにデータを提出していますか?あなたはこれをPHPプログラムからやろうとしていますか?以下は私が何年も前に書いた関数です、あなたが探しているものかどうかわかりません。もしそうでなければ、cURLライブラリを試してみてください。

/* 
    * POST data to a URL with optional auth and custom headers 
    * $URL  = URL to POST to 
    * $DataStream = Associative array of data to POST 
    * $UP   = Optional username:password 
    * $Headers = Optional associative array of custom headers 
    */ 
    function hl_PostIt($URL, $DataStream, $UP='', $Headers = '') { 
    // Strip http:// from the URL if present 
    $URL = preg_replace('=^http://=', '', $URL); 

    // Separate into Host and URI 
    $Host = substr($URL, 0, strpos($URL, '/')); 
    $URI = strstr($URL, '/'); 

    // Form up the request body 
    $ReqBody = ''; 
    while (list($key, $val) = each($DataStream)) { 
     if ($ReqBody) $ReqBody.= '&'; 
     $ReqBody.= $key.'='.urlencode($val); 
    } 
    $ContentLength = strlen($ReqBody); 

    // Form auth header 
    if ($UP) $AuthHeader = 'Authorization: Basic '.base64_encode($UP)."\n"; 

    // Form other headers 
    if (is_array($Headers)) { 
     while (list($HeaderName, $HeaderVal) = each($Headers)) { 
     $OtherHeaders.= "$HeaderName: $HeaderVal\n"; 
     } 
    } 

    // Generate the request 
    $ReqHeader = 
     "POST $URI HTTP/1.0\n". 
     "Host: $Host\n". 
     "User-Agent: PostIt 2.0\n". 
     $AuthHeader. 
     $OtherHeaders. 
     "Content-Type: application/x-www-form-urlencoded\n". 
     "Content-Length: $ContentLength\n\n". 
     "$ReqBody\n"; 

    // Open the connection to the host 
    $socket = fsockopen($Host, 80, $errno, $errstr); 
    if (!$socket) { 
     $Result["errno"] = $errno; 
     $Result["errstr"] = $errstr; 
     return $Result; 
    } 

    // Send the request 
    fputs($socket, $ReqHeader); 

    // Receive the response 
    while (!feof($socket) && $line != "0\r\n") { 
     $line = fgets($socket, 1024); 
     $Result[] = $line; 
    } 

    $Return['Response'] = implode('', $Result); 

    list(,$StatusLine) = each($Result); 
    unset($Result[0]); 
    preg_match('=HTTP/... ([0-9]{3}) (.*)=', $StatusLine, $Matches); 

    $Return['Status'] = trim($Matches[0]); 
    $Return['StatCode'] = $Matches[1]; 
    $Return['StatMesg'] = trim($Matches[2]); 

    do { 
     list($IX, $line) = each($Result); 
     $line = trim($line); 

     unset($Result[$IX]); 

     if (strlen($line)) { 
     list($Header, $Value) = explode(': ', $line, 2); 

     if (isset($Return[$Header])) { 
      if (!is_array($Return[$Header])) { 
      $temp = $Return[$Header]; 
      $Return[$Header] = [$temp]; 
      } 

      $Return[$Header][] = $Value; 
     } 

     else $Return[$Header] = $Value; 
     } 
    } 
    while (strlen($line)); 

    $Return['Body'] = implode('', $Result); 

    return $Return; 
    } 
+0

ここでは、PHPを使用してリクエストを送信していますが、私はC++でリクエストしています。ボットンでフォームを確認するのではなく、C++コードでフォームを実行するので、ボットンで提出する必要はありません。 –

+0

OK、投稿したときにそのタグが表示されませんでした。その場合は、[libcurl](https://curl.haxx.se/libcurl/)または[boost](https://boostgsoc14.github.io/boost.http/)を試してみてください – alanlittle

+0

Yhea yhea man ..私はそれをどのように進めていくのかを学びたいと思っています。ちょうど私は(おそらく...)より高いレベルに行くでしょう。 –

関連する問題