实际上,我想读取搜索查询之后的内容,当它完成时。问题是URL只接受POST方法,它不采取任何行动与GET方法…

我必须在domdocument或file_get_contents()的帮助下读取所有内容。有没有什么方法可以让我用POST方法发送参数,然后通过PHP读取内容?


当前回答

上面的答案对我不起作用。这是第一个完美运行的解决方案:

$sPD = "name=Jacob&bench=150"; // The POST Data
$aHTTP = array(
  'http' => // The wrapper to be used
    array(
    'method'  => 'POST', // Request Method
    // Request Headers Below
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $sPD
  )
);
$context = stream_context_create($aHTTP);
$contents = file_get_contents($sURL, false, $context);

echo $contents;

其他回答

尝试PEAR的HTTP_Request2包来轻松地发送POST请求。或者,您可以使用PHP的curl函数或使用PHP流上下文。

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

PHP5的无卷曲方法:

$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手册,例如:

stream_context_create: http://php.net/manual/en/function.stream-context-create.php

上面的无卷曲方法的另一种替代方法是使用本机流函数:

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) {
    return json_encode(['error' => 'Failed to get contents...']);
  }
  $result = stream_get_contents($fp); // no maxlength/offset
  fclose($fp);
  return $result;
}

如果你碰巧使用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文档。

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

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

// ... your code

它不会显示任何主题或输出任何HTML,只是hack away Wordpress api !

我建议你使用开源包guzzle,它经过了完整的单元测试,并使用了最新的编码实践。

安装狂饮

转到项目文件夹中的命令行并键入以下命令(假设已经安装了包管理器编写器)。如果你需要如何安装Composer的帮助,你应该看看这里。

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);