实际上,我想读取搜索查询之后的内容,当它完成时。问题是URL只接受POST方法,它不采取任何行动与GET方法…
我必须在domdocument或file_get_contents()的帮助下读取所有内容。有没有什么方法可以让我用POST方法发送参数,然后通过PHP读取内容?
实际上,我想读取搜索查询之后的内容,当它完成时。问题是URL只接受POST方法,它不采取任何行动与GET方法…
我必须在domdocument或file_get_contents()的帮助下读取所有内容。有没有什么方法可以让我用POST方法发送参数,然后通过PHP读取内容?
当前回答
[编辑]:请忽略,现在在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);
?>
详情请按此处
其他回答
尝试PEAR的HTTP_Request2包来轻松地发送POST请求。或者,您可以使用PHP的curl函数或使用PHP流上下文。
HTTP_Request2还使模拟服务器成为可能,因此您可以轻松地对代码进行单元测试
我做了一个函数来请求一个使用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;
}
你可以使用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;
?>
上面的答案对我不起作用。这是第一个完美运行的解决方案:
$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;
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