实际上,我想读取搜索查询之后的内容,当它完成时。问题是URL只接受POST方法,它不采取任何行动与GET方法…
我必须在domdocument或file_get_contents()的帮助下读取所有内容。有没有什么方法可以让我用POST方法发送参数,然后通过PHP读取内容?
实际上,我想读取搜索查询之后的内容,当它完成时。问题是URL只接受POST方法,它不采取任何行动与GET方法…
我必须在domdocument或file_get_contents()的帮助下读取所有内容。有没有什么方法可以让我用POST方法发送参数,然后通过PHP读取内容?
尝试PEAR的HTTP_Request2包来轻松地发送POST请求。或者,您可以使用PHP的curl函数或使用PHP流上下文。
HTTP_Request2还使模拟服务器成为可能,因此您可以轻松地对代码进行单元测试
你可以使用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;
?>
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
如果你这样做的话,还有另一个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中。
[编辑]:请忽略,现在在php中不可用。
还有一个你可以用的
<?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);
?>
详情请按此处
如果你碰巧使用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 !
我正在寻找一个类似的问题,并找到了更好的方法来做到这一点。所以它开始了。
您可以简单地在重定向页面(例如page1.php)上放置以下行。
header("Location: URL", TRUE, 307); // Replace URL with to be redirected URL, e.g. final.php
我需要这个来重定向REST API调用的POST请求。这个解决方案能够重定向post数据以及自定义头值。
这里是参考链接。
我使用下面的函数来使用curl发布数据。$data是一个要发布的字段数组(将使用http_build_query()正确编码)。
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进行编码,这可能不可取,因为一些端点希望数据使用application/x-www-form-urlencoded进行编码。当像上面的函数一样使用http_build_query()时,数据将使用application/x-www-form-urlencoded进行编码。
我建议你使用开源包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);
我想补充一些关于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;
}
}
改进
Using http_build_query to get the query-string out of an request-array.(you could also use the array itself, therefore see: http://php.net/manual/en/function.curl-setopt.php) Returning the response instead of echoing it. Btw you can avoid the returning by removing the line curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);. After that the return value is a boolean(true = request was successful otherwise an error occured) and the response is echoed. See: http://php.net/en/manual/function.curl-exec.php Clean session closing and deletion of the curl-handler by using curl_close. See: http://php.net/manual/en/function.curl-close.php Using boolean values for the curl_setopt function instead of using any number.(I know that any number not equal zero is also considered as true, but the usage of true generates a more readable code, but that's just my opinion) Ability to make HTTP-PUT/DELETE calls(useful for RESTful service testing)
用法示例
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"));
删除
$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}]');
}
}
上面的无卷曲方法的另一种替代方法是使用本机流函数:
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;
}
用PHP发送GET或POST请求的更好方法如下:
<?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
这里只使用了一个没有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'
])
]
]));
根据主要答案,以下是我使用的方法:
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...');
我更喜欢这个:
function curlPost($url, $data = NULL, $headers = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
if (curl_error($ch)) {
trigger_error('Curl Error:' . curl_error($ch));
}
curl_close($ch);
return $response;
}
使用的例子:
$response=curlPost("http://my.url.com", ["myField1"=>"myValue1"], ["myFitstHeaderName"=>"myFirstHeaderValue"]);
我做了一个函数来请求一个使用JSON的帖子:
const FORMAT_CONTENT_LENGTH = 'Content-Length: %d';
const FORMAT_CONTENT_TYPE = 'Content-Type: %s';
const CONTENT_TYPE_JSON = 'application/json';
/**
* @description Make a HTTP-POST JSON call
* @param string $url
* @param array $params
* @return bool|string HTTP-Response body or an empty string if the request fails or is empty
*/
function HTTPJSONPost(string $url, array $params)
{
$content = json_encode($params);
$response = file_get_contents($url, false, // do not use_include_path
stream_context_create([
'http' => [
'method' => 'POST',
'header' => [ // header array does not need '\r\n'
sprintf(FORMAT_CONTENT_TYPE, CONTENT_TYPE_JSON),
sprintf(FORMAT_CONTENT_LENGTH, strlen($content)),
],
'content' => $content
]
])); // no maxlength/offset
if ($response === false) {
return json_encode(['error' => 'Failed to get contents...']);
}
return $response;
}
这里有这样的代码:
<?php
$postdata = http_build_query(
array(
'name' => 'Robert',
'id' => '1'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost:8000/api/test', false, $context);
echo $result;?>
上面的答案对我不起作用。这是第一个完美运行的解决方案:
$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;