如何使用PHP发送POST请求?

时间:2011-04-13 10:06:01

标签: php http post request

实际上我想阅读搜索查询后的内容,完成后。问题是该网址只接受POST方法,并且不会采用GET方法执行任何操作...

我必须在domdocumentfile_get_contents()的帮助下阅读所有内容。是否有任何方法可让我使用POST方法发送参数,然后通过PHP阅读内容?

14 个答案:

答案 0 :(得分:1153)

PHP5的CURL-less方法:

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

有关该方法以及如何添加标题的详细信息,请参阅PHP手册,例如:

答案 1 :(得分:95)

您可以使用cURL:

<?php
//The url you wish to send the POST request to
$url = $file_name;

//The data you want to send via POST
$fields = [
    '__VIEWSTATE '      => $state,
    '__EVENTVALIDATION' => $valid,
    'btnSubmit'         => 'Submit'
];

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
$result = curl_exec($ch);
echo $result;
?>

答案 2 :(得分:56)

我使用以下函数使用curl发布数据。 $ data是要发布的字段数组(将使用http_build_query正确编码)。数据使用application / x-www-form-urlencoded进行编码。

function httpPost($url, $data)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

@Edward提到可以省略http_build_query,因为curl会正确编码传递给CURLOPT_POSTFIELDS参数的数组,但是请注意,在这种情况下,数据将使用multipart / form-data进行编码。

我将此函数与API一起使用,期望使用application / x-www-form-urlencoded编码数据。这就是我使用http_build_query()的原因。

答案 3 :(得分:38)

我建议您使用经过完全单元测试的开源软件包guzzle,并使用最新的编码方法。

安装Guzzle

转到项目文件夹中的命令行并输入以下命令(假设您已经安装了包管理器composer)。如果您需要帮助如何安装Composer you should have a look here

php composer.phar require guzzlehttp/guzzle

使用Guzzle发送POST请求

Guzzle的使用非常简单,因为它使用了轻量级的面向对象的API:

// Initialize Guzzle client
$client = new GuzzleHttp\Client();

// Create a POST request
$response = $client->request(
    'POST',
    'http://example.org/',
    [
        'form_params' => [
            'key1' => 'value1',
            'key2' => 'value2'
        ]
    ]
);

// Parse the response object, e.g. read the headers, body, etc.
$headers = $response->getHeaders();
$body = $response->getBody();

// Output headers and body for debugging purposes
var_dump($headers, $body);

答案 4 :(得分:24)

如果你这样做,还有另一种CURL方法。

一旦你了解PHP curl扩展的工作方式,将各种标志与setopt()调用相结合,这非常简单。在这个例子中,我有一个变量$ xml,它包含我准备发送的XML - 我将把它的内容发布到示例的测试方法。

$url = 'http://api.example.com/services/xmlrpc/';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
//process $response

首先我们初始化连接,然后使用setopt()设置一些选项。这些告诉PHP我们正在发布一个post请求,并且我们正在发送一些数据,提供数据。 CURLOPT_RETURNTRANSFER标志告诉curl将输出作为curl_exec的返回值而不是输出它。然后我们进行调用并关闭连接 - 结果是$ response。

答案 5 :(得分:19)

如果您有任何机会使用Wordpress开发您的应用程序(它实际上是获取授权,信息页面等方便的方式,即使是非常简单的内容),您可以使用以下代码段:

$response = wp_remote_post( $url, array('body' => $parameters));

if ( is_wp_error( $response ) ) {
    // $response->get_error_message()
} else {
    // $response['body']
}

它使用不同的方式来发出实际的HTTP请求,具体取决于Web服务器上的可用内容。有关详细信息,请参阅HTTP API documentation

如果您不想开发自定义主题或插件来启动Wordpress引擎,您可以在wordpress根目录中的独立PHP文件中执行以下操作:

require_once( dirname(__FILE__) . '/wp-load.php' );

// ... your code

它不会显示任何主题或输出任何HTML,只需破解Wordpress API!

答案 6 :(得分:16)

我想补充一些关于Fred Tanrikut基于卷曲的答案的想法。我知道他们中的大多数已经写在上面的答案中,但我认为最好显示一个包含所有这些答案的答案。

这是我写的基于curl发出HTTP-GET / POST / PUT / DELETE请求的类,关于响应体:

class HTTPRequester {
    /**
     * @description Make HTTP-GET call
     * @param       $url
     * @param       array $params
     * @return      HTTP-Response body or an empty string if the request fails or is empty
     */
    public static function HTTPGet($url, array $params) {
        $query = http_build_query($params); 
        $ch    = curl_init($url.'?'.$query);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
    /**
     * @description Make HTTP-POST call
     * @param       $url
     * @param       array $params
     * @return      HTTP-Response body or an empty string if the request fails or is empty
     */
    public static function HTTPPost($url, array $params) {
        $query = http_build_query($params);
        $ch    = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
    /**
     * @description Make HTTP-PUT call
     * @param       $url
     * @param       array $params
     * @return      HTTP-Response body or an empty string if the request fails or is empty
     */
    public static function HTTPPut($url, array $params) {
        $query = \http_build_query($params);
        $ch    = \curl_init();
        \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
        \curl_setopt($ch, \CURLOPT_HEADER, false);
        \curl_setopt($ch, \CURLOPT_URL, $url);
        \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT');
        \curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);
        $response = \curl_exec($ch);
        \curl_close($ch);
        return $response;
    }
    /**
     * @category Make HTTP-DELETE call
     * @param    $url
     * @param    array $params
     * @return   HTTP-Response body or an empty string if the request fails or is empty
     */
    public static function HTTPDelete($url, array $params) {
        $query = \http_build_query($params);
        $ch    = \curl_init();
        \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
        \curl_setopt($ch, \CURLOPT_HEADER, false);
        \curl_setopt($ch, \CURLOPT_URL, $url);
        \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE');
        \curl_setopt($ch, \CURLOPT_POSTFIELDS, $query);
        $response = \curl_exec($ch);
        \curl_close($ch);
        return $response;
    }
}

改进

  • 使用http_build_query从请求数组中获取查询字符串。(您也可以使用数组本身,因此请参阅:http://php.net/manual/en/function.curl-setopt.php
  • 返回响应而不是回显它。顺便说一句,你可以通过删除行 curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); 来避免返回。之后,返回值为布尔值(true =请求成功,否则发生错误)并回显响应。 请参阅:http://php.net/en/manual/function.curl-exec.php
  • 使用 curl_close 清除会话结束和删除卷曲处理程序。请参阅:http://php.net/manual/en/function.curl-close.php
  • 使用 curl_setopt 函数的布尔值而不是使用任何数字。(我知道任何不等于零的数字也被视为true,但使用true会生成更易读的代码,但是那只是我的意见)
  • 能够进行HTTP-PUT / DELETE调用(对RESTful服务测试很有用)

使用示例

GET

$response = HTTPRequester::HTTPGet("http://localhost/service/foobar.php", array("getParam" => "foobar"));

POST

$response = HTTPRequester::HTTPPost("http://localhost/service/foobar.php", array("postParam" => "foobar"));

PUT

$response = HTTPRequester::HTTPPut("http://localhost/service/foobar.php", array("putParam" => "foobar"));

DELETE

$response = HTTPRequester::HTTPDelete("http://localhost/service/foobar.php", array("deleteParam" => "foobar"));

测试

您还可以使用这个简单的类进行一些很酷的服务测试。

class HTTPRequesterCase extends TestCase {
    /**
     * @description test static method HTTPGet
     */
    public function testHTTPGet() {
        $requestArr = array("getLicenses" => 1);
        $url        = "http://localhost/project/req/licenseService.php";
        $this->assertEquals(HTTPRequester::HTTPGet($url, $requestArr), '[{"error":false,"val":["NONE","AGPL","GPLv3"]}]');
    }
    /**
     * @description test static method HTTPPost
     */
    public function testHTTPPost() {
        $requestArr = array("addPerson" => array("foo", "bar"));
        $url        = "http://localhost/project/req/personService.php";
        $this->assertEquals(HTTPRequester::HTTPPost($url, $requestArr), '[{"error":false}]');
    }
    /**
     * @description test static method HTTPPut
     */
    public function testHTTPPut() {
        $requestArr = array("updatePerson" => array("foo", "bar"));
        $url        = "http://localhost/project/req/personService.php";
        $this->assertEquals(HTTPRequester::HTTPPut($url, $requestArr), '[{"error":false}]');
    }
    /**
     * @description test static method HTTPDelete
     */
    public function testHTTPDelete() {
        $requestArr = array("deletePerson" => array("foo", "bar"));
        $url        = "http://localhost/project/req/personService.php";
        $this->assertEquals(HTTPRequester::HTTPDelete($url, $requestArr), '[{"error":false}]');
    }
}

答案 7 :(得分:12)

curl-less method above的另一个替代方法是使用本机函数:

  • stream_context_create()
      

    使用 options 预设中提供的任何选项创建并返回流上下文。

  • stream_get_contents()
      

    file_get_contents()相同,但stream_get_contents() 对已打开的流资源进行操作,并返回字符串中的剩余内容,最多为 maxlength 字节,从指定的偏移开始。

使用这些功能的POST功能可以是这样的:

<?php

function post_request($url, array $params) {
  $query_content = http_build_query($params);
  $fp = fopen($url, 'r', FALSE, // do not use_include_path
    stream_context_create([
    'http' => [
      'header'  => [ // header array does not need '\r\n'
        'Content-type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($query_content)
      ],
      'method'  => 'POST',
      'content' => $query_content
    ]
  ]));
  if ($fp === FALSE) {
    fclose($fp);
    return json_encode(['error' => 'Failed to get contents...']);
  }
  $result = stream_get_contents($fp); // no maxlength/offset
  fclose($fp);
  return $result;
}

答案 8 :(得分:5)

还有一个你可以使用

<?php
$fields = array(
    'name' => 'mike',
    'pass' => 'se_ret'
);
$files = array(
    array(
        'name' => 'uimg',
        'type' => 'image/jpeg',
        'file' => './profile.jpg',
    )
);

$response = http_post_fields("http://www.example.com/", $fields, $files);
?>

Click here for details

答案 9 :(得分:5)

我一直在寻找类似的问题,并找到了更好的方法。所以就这样了。

您只需将以下行放在重定向页面上(例如page1.php)。

header("Location: URL", TRUE, 307); // Replace URL with to be redirected URL, e.g. final.php

我需要这个来重定向REST API调用的POST请求。此解决方案能够使用发布数据和自定义标头值重定向。

这是the reference link

答案 10 :(得分:2)

通过GET发送POSTPHP请求的更好方法如下:

<?php
    $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
    $r->setOptions(array('cookies' => array('lang' => 'de')));
    $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));

    try {
        echo $r->send()->getBody();
    } catch (HttpException $ex) {
        echo $ex;
    }
?>

此代码取自http://docs.php.net/manual/da/httprequest.send.php此处的官方文档

答案 11 :(得分:1)

尝试使用PEAR的HTTP_Request2包轻松发送POST请求。或者,您可以使用PHP的curl函数或使用PHP stream context

HTTP_Request2还可以模拟服务器,因此您可以轻松地对代码进行单元测试

答案 12 :(得分:1)

此处仅使用一个不带cURL的命令。超级简单。

echo file_get_contents('https://www.server.com', false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header'  => "Content-type: application/x-www-form-urlencoded",
        'content' => http_build_query([
            'key1' => 'Hello world!', 'key2' => 'second value'
        ])
    ]
]));

答案 13 :(得分:0)

基于主要答案,这是我使用的:

function do_post($url, $params) {
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => $params
        )
    );
    $result = file_get_contents($url, false, stream_context_create($options));
}

用法示例:

do_post('https://www.google-analytics.com/collect', 'v=1&t=pageview&tid=UA-xxxxxxx-xx&cid=abcdef...');