プロセスはチケットをサーバーに送信し、サーバーは必要な情報で応答します。例えば、Pythonを使用してサーバーにキーと値のペアとバイナリ情報を一緒に送信する方法
import requests
URL = "https://ssl.XXX.com/sql_v5i.php"
Ticket = {"A":"1","B":"2"} # a dictionary
ticket_post = requests.post(URL, data=Ticket)
print ticket__post.text
しかし、どのように一度に全部(データのみなしキー付き)"A":"1", "B":"2"
とバイナリデータでチケットを送信するには?
<?php
$BASE_SERVER_URL="https://ssl.XXX.com/sql_v5i.php"
$TICKET = array(
'fn' => 'ticket',
'testid' => '2'
... # And many other key-value pair in the TICKET array
);
class DataStreamer{
private $data;
private $pos;
function __construct($data) {
$this->data = $data;
$this->pos = 0;
}
function stream_function($handle, $fd, $count){
$res = substr($this->data, $this->pos, $count);
$this->pos += strlen($res);
return $res;
}
}
function sendTicketToServer($data) {
global $TICKET, $BASE_SERVER_URL; # TICKET is an array stores ticket
# information in php
$ret = array(true);
$postFields = "";
foreach ($TICKET as $name => $val)
$postFields = (empty($postFields) ? "" : "${postFields}&") ."${name}=" . urlencode($val);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, "${BASE_SERVER_URL}?" . $postFields);
# add key-value pairs
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) { # data is the binary string , add binary string
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/octet-stream', 'Content-length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_READFUNCTION, array(new DataStreamer($data), "stream_function"));
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/octet-stream', 'Content-length: 0'));
}
$resp = curl_exec($ch);
$code=curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($resp === FALSE || $code !== 200) {
echo "!!!!! Failed to get connection from the server. Code: $code\n";
return array(ERR_NO_SERVER_CONNECTION."000");
}
curl_close($ch);
return $ret;
はPythonでこれと同じ操作を行う方法(要求を使用するか、またはその両方許容されない):
は、PHPの作品の一部が存在し、このタスクをcomoleteします。binary_data = " 5UU ǚ h V "
_Pyghon_はある種のライブラリですか? – AbraCadaver
'PHP'は' Ticket'をPOSTデータとして送りませんが、URLに '$ {BASE_SERVER_URL}? ' 。 $ postFields' - それはurl 'https://ssl.XXX.com/sql_v5i.php?A = 1&B = 2'を使用します。 '要求'に 'params = Ticket'を使って同じことをすることができます。そして、あなたは 'data ='バイナリデータを送ることができます。 – furas