我想将数据发送到 API。数据包括简单变量:用户名、密码、电子邮件等。 问题是 O 想使用 POST 方法向它发送数据。我在 Google 上搜索了这个问题,每个人都说要使用 CURL。
什么是 CURL?它是函数、脚本、API 还是什么?
还有其他方法吗?
我想发送这样的东西:
$username, $password to www.abc.com?username=$username&password=$password
最好的问候
最佳答案
通过使用此功能,您可以发送带有可选数量的帖子请求
参数。只需用键值对填充 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);
?>
关于php - 使用不带表单的 Post 方法发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6757569/