php 如何在不卷曲的情况下发出帖子请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8596311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a post request without curl?
提问by dynamic
Considering I don't want to use curl, which is a valid alternative to make a post request? Maybe Zend_http_client
?
考虑到我不想使用 curl,这是发出 post 请求的有效替代方法吗?也许Zend_http_client
?
I just need the basic stuff (I need to request an url with only one post param)
我只需要基本的东西(我需要请求一个只有一个帖子参数的网址)
采纳答案by Daff
If you are using the Zend Framework already you should try the Zend_Http_Clientyou mentioned:
如果您已经在使用 Zend 框架,您应该尝试您提到的Zend_Http_Client:
$client = new Zend_Http_Client($host, array(
'maxredirects' => 3,
'timeout' => 30));
$client->setMethod(Zend_Http_Client::POST);
// You might need to set some headers here
$client->setParameterPost('key', 'value');
$response = $client->request();
回答by Audun Larsen
You could use file_get_contents().
您可以使用file_get_contents()。
The PHP manual has a nice example here. This is just copy past from the manual:
PHP 手册在这里有一个很好的例子。这只是从手册中复制过去:
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$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://example.com/submit.php', false, $context);
回答by Saurabh Chandra Patel
you can use stream_context_createand file_get_contents
您可以使用stream_context_create和file_get_contents
<?php
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
?>
$context = stream_context_create($context_options);
$data = file_get_contents('http://www.php.net', false, $context);
回答by TimWolla
You can implement it yourself via sockets:
您可以通过套接字自己实现它:
$url = parse_url(''); // url
$requestArray = array('var' => 'value');
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, $url['host'], ((isset($url['port'])) ? $url['port'] : 80));
if (!$sock) {
throw new Exception('Connection could not be established');
}
$request = '';
if (!empty($requestArray)) {
foreach ($requestArray as $k => $v) {
if (is_array($v)) {
foreach($v as $v2) {
$request .= urlencode($k).'[]='.urlencode($v2).'&';
}
}
else {
$request .= urlencode($k).'='.urlencode($v).'&';
}
}
$request = substr($request,0,-1);
}
$data = "POST ".$url['path'].((!empty($url['query'])) ? '?'.$url['query'] : '')." HTTP/1.0\r\n"
."Host: ".$url['host']."\r\n"
."Content-type: application/x-www-form-urlencoded\r\n"
."User-Agent: PHP\r\n"
."Content-length: ".strlen($request)."\r\n"
."Connection: close\r\n\r\n"
.$request."\r\n\r\n";
socket_send($sock, $data, strlen($data), 0);
$result = '';
do {
$piece = socket_read($sock, 1024);
$result .= $piece;
}
while($piece != '');
socket_close($sock);
// TODO: Add Header Validation for 404, 403, 401, 500 etc.
echo $result;
Of course you have to change it to fill your needs or wrap it into a function.
当然,您必须更改它以满足您的需求或将其包装成一个函数。
回答by Eric Kigathi
The simplest way if you have PHP configured with pecl_http is:
如果您使用 pecl_http 配置了 PHP,最简单的方法是:
$response = http_post_data($url, $post_params_string);
$response = http_post_data($url, $post_params_string);
The function is documented on php.net:
该函数记录在 php.net 上:
- Documentation http://php.net/manual/en/function.http-post-data.php
- Install: http://php.net/manual/en/http.install.php
PECL also provides a well documented way to handle Cookies, Redirects, Authentication etc before the POST:
PECL 还提供了一种有据可查的方法来在 POST 之前处理 Cookie、重定向、身份验证等:
回答by kclair
RESTclient is a nice little app for this: http://code.google.com/p/rest-client/
RESTclient 是一个不错的小应用程序:http: //code.google.com/p/rest-client/