CodeIgniter 将 POST 数据发送到特定的 URL - API 数据
CodeIgniter sending POST data to specific URL - API data
我需要将数据发送到 URL,我之前已通过方法 GET
和使用 url_encode()
完成了此操作
现在我需要 POST 使用框架 CodeIgniter,我不太清楚该用什么
任何帮助将不胜感激
Ok, posting answer for avoiding time lapse for searching solution in
future
目标: 发送 POST 数据到指定的 URL 即 API
框架: Codeigniter
解法:
假设需要传递的数据是一个包含姓名、电子邮件、密码的数组
$params= array(
"name" => $_name,
"email" => $_email,
"password" => $_pass
);
$url = '192.168.100.51/AndroidApi/v2/register';
echo '<br><hr><h2>'.$this->postCURL($url, $params).'</h2><br><hr><br>';
通过 POST 将数据发送到 url 的功能:是的,我们将在此处使用 cURL
public function postCURL($_url, $_param){
$postData = '';
//create name value pairs seperated by &
foreach($_param as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
测试cURL是否可用
echo (is_callable('curl_init')) ? '<h1>Enabled</h1>' : '<h1>Not enabled</h1>' ;
谢谢,输出效果很好
Failure :
{"error":true,"message":"Email address is not valid"}
OR Success :
{"error":false,"message":"You are successfully registered"}
我需要将数据发送到 URL,我之前已通过方法 GET
和使用 url_encode()
现在我需要 POST 使用框架 CodeIgniter,我不太清楚该用什么
任何帮助将不胜感激
Ok, posting answer for avoiding time lapse for searching solution in future
目标: 发送 POST 数据到指定的 URL 即 API
框架: Codeigniter
解法:
假设需要传递的数据是一个包含姓名、电子邮件、密码的数组
$params= array(
"name" => $_name,
"email" => $_email,
"password" => $_pass
);
$url = '192.168.100.51/AndroidApi/v2/register';
echo '<br><hr><h2>'.$this->postCURL($url, $params).'</h2><br><hr><br>';
通过 POST 将数据发送到 url 的功能:是的,我们将在此处使用 cURL
public function postCURL($_url, $_param){
$postData = '';
//create name value pairs seperated by &
foreach($_param as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
测试cURL是否可用
echo (is_callable('curl_init')) ? '<h1>Enabled</h1>' : '<h1>Not enabled</h1>' ;
谢谢,输出效果很好
Failure :
{"error":true,"message":"Email address is not valid"}
OR Success :
{"error":false,"message":"You are successfully registered"}