为什么数据传输后出现错误

时间:2019-05-20 07:57:11

标签: php data-transfer

我有两个脚本,这些脚本无需使用浏览器即可在两个域之间交换数据。发件人总是告诉我数据无法正确发送。但是由于我没有收到error1消息,因此连接本身应该正确建立。

奇怪的是,当两个脚本都位于同一主域的不同子域上时,它已经起作用了。现在,我将发件人移到了另一个域的子域,现在得到了错误。两个域都位于具有相同IP的同一台服务器上,并且当然都具有ssl证书。

这是发送脚本:

    $fp = fsockopen("ssl://check.target.com", 443, $errno, $errstr, 30);
    if (!$fp){
        die('error1');
    }else{
        $path = '/api/vk_register.php';
        $host = 'https://check.target.com';
        $referer = $_SERVER['SERVER_NAME'];
        fputs($fp, "POST /$path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Referer: $referer\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $data_to_send);

        while (!feof($fp))
            $return[] = fgets($fp);
        fclose($fp);
    // handling of $return
    }

这是接收者:

    $token = $_POST['token'];
    $firstname = urldecode($_POST['firstname']);
    $name = urldecode($_POST['name']);
    $vkkdnr = $_POST['vkkdnr'];
    $checksum = $_POST['checksum'];
    $sender = $_SERVER["HTTP_REFERER"];

    // handling the transferred data //

    $fp = fsockopen($sender, 443, $errno, $errstr, 30);
    if (!$fp){
        echo "$errstr ($errno)<br />\n";
    }else{
        $path = '/ptreg_send.php';
        $host = $sender;
        $referer = $_SERVER['SERVER_NAME'];
        fputs($fp, "POST /$path HTTP/1.1\r\n");
        fputs($fp, "Host: $host\r\n");
        fputs($fp, "Referer: $referer\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: ". strlen($data_to_send) ."\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $data_to_send);

        while (!feof($fp)){
            $return2[] = fgets($fp,128)."#";
        }
        fclose($fp);
    }

打印$ return2会带来以下效果:

Array
(
    [0] => HTTP/1.1 400 Bad Request
    [1] => Date: Fri, 17 May 2019 15:03:33 GMT
    [2] => Server: Apache
    [3] => Content-Length: 347
    [4] => Connection: close
    [5] => Content-Type: text/html; charset=iso-8859-1
    [6] =>
    [7] => <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    [8] => <html><head>
    [9] => <title>400 Bad Request</title>
    [10] => </head><body>
    [11] => <h1>Bad Request</h1>
    [12] => <p>Your browser sent a request that this server could not understand.<br />
    [13] => </p>
    [14] => <p>Additionally, a 400 Bad Request
    [15] => error was encountered while trying to use an ErrorDocument to handle the request.</p>
    [16] => </body></html>
)

1 个答案:

答案 0 :(得分:0)

当两个脚本开始时位于同一域中时,这样给出路径就足够了:

    $path = '/api/vk_register.php';

但是,当脚本位于不同的域时,您需要提供具有完整域的路径:

    $path = 'https://example.com/api/vk_register.php';