php 使用不带表单的 post 方法发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6757569/
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
Sending Data Using Post Method Without Form
提问by theGame
I want to send data to an API. The data include simple variables: username, password, email, etc. The problem is that O want to send data to this using POST method. I searched this issue on Google, and everyone is saying to go for CURL.
我想将数据发送到 API。数据包括简单的变量:用户名、密码、电子邮件等。问题是 O 想使用 POST 方法向 this 发送数据。我在谷歌上搜索了这个问题,每个人都说去 CURL。
What is CURL? Is it a function, script, API or what?
什么是卷曲?它是一个函数、脚本、API 还是什么?
Is there any other way to do it?
有没有其他方法可以做到?
I want to send something like this:
我想发送这样的东西:
$username, $password to www.abc.com?username=$username&password=$password
Best Regards
此致
回答by JK.
By using this function you can send a post request with an optional number of
parameters. Just fill in the postdata array with key values pairs.
Example usage are provided.
通过使用此函数,您可以发送带有可选参数数量的 post 请求。只需用键值对填充 postdata 数组。提供了示例用法。
<?php
function do_post_request($url, $postdata)
{
$content = "";
// Add post data to request.
foreach($postdata as $key => $value)
{
$content .= "{$key}={$value}&";
}
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $content
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Connection problem, {$php_errormsg}");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Response error, {$php_errormsg}");
}
return $response;
}
// Post variables.
$postdata = array(
'username' => 'value1',
'password' => 'value2',
'param3' => 'value3'
);
do_post_request("http://www.example.com", $postdata);
?>
回答by fromvega
cURL is a library "that allows you to connect and communicate to many different types of servers with many different types of protocols". So you can use cURL to send an HTTP POST request. Please read the PHP manual for detailed information: http://www.php.net/manual/en/book.curl.php
cURL 是一个库,“它允许您使用许多不同类型的协议连接和通信到许多不同类型的服务器”。因此,您可以使用 cURL 发送 HTTP POST 请求。详细信息请阅读PHP手册:http: //www.php.net/manual/en/book.curl.php
But cURL is not the only answer. You can use PHP Streams, or even connect to the webserver using socket functions and then generate your requests.
但是 cURL 并不是唯一的答案。您可以使用PHP Streams,甚至可以使用套接字函数连接到网络服务器,然后生成您的请求。
Personally I often use cURL because it's very flexible and you can work with cookies easily.
就我个人而言,我经常使用 cURL,因为它非常灵活,您可以轻松使用 cookie。
回答by wpcoder
Simply use: file_get_contents()
只需使用: file_get_contents()
// building array of variables
$content = http_build_query(array(
'username' => 'value',
'password' => 'value'
));
// creating the context change POST to GET if that is relevant
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'content' => $content, )));
$result = file_get_contents('http://www.example.com', null, $context);
//dumping the reuslt
var_dump($result);
回答by Ozair Kafray
Php curl manualis a good starting point.
PHP curl 手册是一个很好的起点。
Following is an example of the usage of curl in php on Stackoverflow:
以下是Stackoverflow上php中curl的使用示例:
回答by Vadim
The data set in an xml file:
xml文件中的数据集:
$post="<?xml version="1.0\" encoding=\"UTF-8\"?>
<message xmlns=\"url\" >
<username>".$username."</username>
<password>".$password."</password>
<status date=\"2010-11-05 14:44:10+02\"/>
<body content-type=\"text/plain\">Noobligatory text</body>
</message>";
$url = "a-url-to-send-the-data";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);