11

fsockopenを使用してデータを投稿しようとしていますが、結果が返されます。これが私の現在のコードです:

<?php
$data="stuff=hoorah\r\n";
$data=urlencode($data);

$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "POST /script.php HTTP/1.0\r\n";
    $out .= "Host: www.webste.com\r\n";
    $out .= 'Content-Type: application/x-www-form-urlencoded\r\n';
    $out .= 'Content-Length: ' . strlen($data) . '\r\n\r\n';
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?> 

ページをエコーすることになっていて、ページをエコーし​​ていますが、script.phpのスクリプトは次のとおりです。

<?php
echo "<br><br>";    
$raw_data = $GLOBALS['HTTP_RAW_POST_DATA'];  
 parse_str( $raw_data, $_POST );

//test 1
var_dump($raw_data);
echo "<br><br>":
//test 2
print_r( $_POST );  
?>

結果は次のとおりです。

HTTP / 1.1 200 OK日付:2010年3月2日火曜日22:40:46 GMTサーバー:Apache / 2.2.3(CentOS)X-Powered-By:PHP / 5.2.6 Content-Length:31接続:closeContent-Type :text / html; charset = UTF-8 string(0) ""配列()

何が間違っていますか?変数がデータを投稿しないのはなぜですか?

4

10 に答える 10

19

あなたのコードには多くの小さなエラーがあります。テスト済みで動作するスニペットを次に示します。

<?php

$fp = fsockopen('example.com', 80);

$vars = array(
    'hello' => 'world'
);
$content = http_build_query($vars);

fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");
fwrite($fp, "Host: example.com\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");

fwrite($fp, $content);

header('Content-type: text/plain');
while (!feof($fp)) {
    echo fgets($fp, 1024);
}

そして、example.com/reposter.php にこれを置きます

<?php print_r($_POST);

実行すると、次のような出力が得られるはずです

HTTP/1.1 200 OK
Date: Wed, 05 Jan 2011 21:24:07 GMT
Server: Apache
X-Powered-By: PHP/5.2.9
Vary: Host
Content-Type: text/html
Connection: close

1f
Array
(
    [hello] => world
)
0
于 2011-01-05T21:40:44.600 に答える
1

代わりにこれを試してください

$out .= 'Content-Length: ' . strlen($data) . '\r\n';
$out .= "Connection: Close\r\n\r\n";
$out .= $data;
于 2010-03-02T23:30:55.473 に答える
1

これを試して:

<?php
$data="stuff=hoorah\r\n";
$data=urlencode($data);

$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "POST /script.php HTTP/1.0\r\n";
    $out .= "Host: www.webste.com\r\n";
    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out .= 'Content-Length: ' . strlen($data) . "\r\n\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    fwrite($fp, $data);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?> 

などの一部の文字エスケープは\n、一重引用符では機能しません。

于 2010-03-05T18:55:06.177 に答える
1

$dataソケットに書き込まれることはありません。次のようなものを追加します。

$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
fwrite($fp, $data);
于 2010-03-02T23:01:31.257 に答える
0

素敵なタムリン、素晴らしい作品です!

URL とともに get vars も送信する必要がある場合は、

//change this:

fwrite($fp, "POST /reposter.php HTTP/1.1\r\n");

//to:

$query = 'a=1&b=2';
fwrite($fp, "POST /reposter.php?".$query." HTTP/1.1\r\n");
于 2011-03-23T15:26:28.980 に答える
0

この手法を使用すると、好きなだけページを呼び出すのに役立ちます。すべてのページは、各ページの応答を非同期として待つことなく、一度に独立して実行されます。

cornjobpage.php //メインページ

    <?php

post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue");
//post_async("http://localhost/projectname/testpage.php", "Keywordname=testValue2");
//post_async("http://localhost/projectname/otherpage.php", "Keywordname=anyValue");
//call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
            ?>
            <?php

            /*
             * Executes a PHP page asynchronously so the current page does not have to wait for it to     finish running.
             *  
             */
            function post_async($url,$params)
            {

                $post_string = $params;

                $parts=parse_url($url);

                $fp = fsockopen($parts['host'],
                    isset($parts['port'])?$parts['port']:80,
                    $errno, $errstr, 30);

                $out = "POST ".$parts['path']."?$post_string"." HTTP/1.1\r\n";//you can use GET instead of POST if you like
                $out.= "Host: ".$parts['host']."\r\n";
                $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
                $out.= "Content-Length: ".strlen($post_string)."\r\n";
                $out.= "Connection: Close\r\n\r\n";
                fwrite($fp, $out);
                fclose($fp);
            }
            ?>

testpage.php

    <?
    echo $_REQUEST["Keywordname"];//case1 Output > testValue
    ?>

PS: URL パラメータをループとして送信する場合は、次の回答に従ってください: https://stackoverflow.com/a/41225209/6295712

于 2016-12-19T15:16:40.307 に答える
0

reposter.php でこれを試してください

$raw_data = $GLOBALS['HTTP_RAW_POST_DATA']; 
parse_str( $raw_data, $_POST );

print_r( $_POST );

なぜなら、データは変数にはありませんでしたが、$_POST[]変数にはありました$GLOBALS['HTTP_RAW_POST_DATA']

于 2011-12-08T03:52:54.553 に答える
-1

post_to_host() を使用するには Curl が重すぎる場合があります。

//GET:
$str_rtn=post_to_host($str_url_target, array(), $arr_cookie, $str_url_referer, $ref_arr_head, 0);

//POST:
$arr_params=array('para1'=>'...', 'para2'=>'...');
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head);

//POST with file:
$arr_params=array('para1'=>'...', 'FILE:para2'=>'/tmp/test.jpg', 'para3'=>'...');
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head, 2);

//raw POST:
$tmp=array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile())));
$arr_params=array('para1'=>'...', 'para2'=>'...');
file_put_contents($tmp, json_encode($arr_params));
$arr_params=array($tmp);
$str_rtn=post_to_host($str_url_target, $arr_params, $arr_cookie, $str_url_referer, $ref_arr_head, 3);

//get cookie and merge cookies:
$arr_new_cookie=get_cookies_from_heads($ref_arr_head)+$arr_old_cookie;//don't change the order

//get redirect url:
$str_url_redirect=get_from_heads($ref_arr_head, 'Location');

php プロジェクトの場所をホストする投稿: http://code.google.com/p/post-to-host/

于 2012-01-05T06:52:20.840 に答える
-2

更新して申し訳ありませんが、それでもこのような問題が発生する場合は、HTTP/1.0をHTTP/1.1に変更してください。

于 2012-11-07T09:48:01.257 に答える
-2

cURLとオプションを使用していますか?

于 2010-03-02T23:47:47.150 に答える