我想将每个 HTTP 请求 3 个变量发送到另一个站点并希望获得响应。
检查.php:
<html><body>
<form action="response.php" method="post">
<p>Variable1: <input type="text" name="var1" /></p>
<p>Vairable2: <input type="text" name="var2" /></p>
<p><input type="submit" /></p>
</form>
</body></html>
响应.php:
<?php
$url = "http://www.testpage/api.php?authcode=";
$apikey = "xxxxxxx" ;
$request = new HTTPRequest($url$apikey&$var1&$var2, HTTP_METH_POST); //req not work !
$request->send(); //nothing happens
response = $request->getResponseBody(); //only
//got
echo "$response "; //500 error
?>
试试这个:
$url = "http://www.testpage.com/api.php";
$r = new HTTPRequest($url, HTTP_METH_POST);
$r->addPostFields(array('authcode' => $apikey, 'var1' => '', 'var2' => ''));
$r->send();
将数组中的空字段替换为 var1 和 var2 的值。
此外,您可能想使用 cURL 而不是内置的 HTTP 处理程序。 PHP +curl,HTTP POST 示例代码?