【问题标题】:PHP HTTP Request with more than one Variables?具有多个变量的 PHP HTTP 请求?
【发布时间】:2013-01-25 13:31:53
【问题描述】:

您好,我想将每个 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>

response.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 . $apikey . '&amp;' . $var1 . '&amp;' . $var2

标签: php api url variables httprequest


【解决方案1】:

试试这个:

$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 sample code?

【讨论】:

  • 您好,感谢您的回答和提示,但我仍然收到 500 错误。我现在正在尝试 curl 示例。
猜你喜欢
  • 2011-01-18
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 2018-11-09
  • 2017-05-03
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
相关资源
最近更新 更多