如何从PHP代码中轻松地进行POST?

时间:2012-11-14 22:13:12

标签: php http-post

  

可能重复:
  PHP + curl, HTTP POST sample code?
  POST data to URL php
  How to issue HTTP POST request?

我创建了一个拦截常规帖子的脚本,并根据用户名做了一件事。

在其中一个条件分支中,我想移动用户并将他在登录表单中记下的内容发送到另一个PHP脚本。

如何轻松地向URL发出POST请求?

是否有类似的东西:

$data = array("username" => "admin", "password" => "hunter2");

http_post("foo.com", "POST", $data);

有什么建议吗?

2 个答案:

答案 0 :(得分:6)

像这样:

// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');

// Build Http query using params
$query = http_build_query ($params);

// Create Http context details
$contextData = array (
            'method' => 'POST',
            'header' => "Connection: close\r\n".
                        "Content-Length: ".strlen($query)."\r\n",
            'content'=> $query );

// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));

// Read page rendered as result of your POST request
$result =  file_get_contents (
              'http://www.sample-post-page.com',  // page url
              false,
              $context);

// Server response is now stored in $result variable so you can process it

源: http://fczaja.blogspot.ch/2011/07/php-how-to-send-post-request-with.html

答案 1 :(得分:-1)

这就是卷曲功能的存在。例如,请参阅:http://davidwalsh.name/curl-post