对来自PHP客户端站点的Https POST请求做出响应

时间:2012-10-11 11:18:53

标签: php post ssl https openssl

我有一个使用SSL保护的中央网站(A)。

其他客户网站(比如B,C,D,E .....)将连接到此网站A.

要求是从站点B(或C,D,E .....)向站点A发送HTTPS POST请求。

请求的参数为

WEBSITEKEY=keyvalue;

然后,站点A必须对此请求作出响应。

响应应具有必要的一个参数,

SECUREKEY=key-value-secure;
Para1=para1value;
Para2=para2value;
Para3=para3value;
Para4=para4value;

如何在PHP中实现此响应部分?我是SSL POST和响应的新手。

稍后编辑,

我们总是发送请求并获得即将到来的响应。 在这里,我需要给出响应/创建客户端请求的响应。 我怎么能用PHP做到这一点? 我找到了以下代码..

<?php
//process the request by fetching the info
$headers = http_get_request_headers();
$result = http_get_request_body();
//do stuff with the $headers and $result variables....
//then send your response
HttpResponse::status(200);
HttpResponse::setContentType('text/xml');
HttpResponse::setHeader('From', 'Lymber');
HttpResponse::setData('<?xml version="1.0"?><note>Thank you for posting your data! We    love php!</note>');
HttpResponse::send();
?>    

将我的SSL启用站点的响应发送到正常站点是否正确?

1 个答案:

答案 0 :(得分:4)

SSL部分无关紧要;它将由Web服务器处理,PHP将照常接收请求。

您唯一需要做的就是构建您的请求,为此,您需要知道此请求将要包含的格式。此外,您是否需要一些特殊的MIME类型或者其他什么。

例如,假设答案必须是JSON。然后它需要像:

<?php
    if (!isset($_POST['WEBSITEKEY'])) {
         // Handle error. E.g.
         Header("HTTP/1.1 400 Bad Request");
         die();
    }

    $websitekey = $_POST['WEBSITEKEY'];

    require 'yourcode.php';

    $response = array(
        'SECUREKEY' => md5($secret . $websitekey),
        'para1'     => date('Y-m-d H:i:s'),
        'para2'     => 'Hello world',
    );
    // Specifying charset isn't strictly necessary but may be useful.
    Header("Content-Type: application/json;charset=UTF-8");
    $data = json_encode($response);
    $len  = strlen($data);
    // Some browser/proxy/load balancer may get better performance if the
    // length is known beforehand. This also disables Chunked-Encoding, which
    // in some scenarios may give problems.
    Header("Content-Length: {$len}");
    die($data); // Ensure no more output after this..

如何处理$websitekey完全取决于您的应用程序逻辑。在这里,它连接到一个秘密盐字符串并用于构建响应SECUREKEY,但你基本上可以做任何你想做的事。