我有以下代码:
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'test@test.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'from@from.com'
);
/* Put the below into a function */
$request = 'api url goes here';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
print_r($response);
/* Put the above into a function */
基本上我想把上面的代码放在一个函数的注释之间,以便我可以设置params然后执行以下操作:
sendthat($params);
这只是简单地清理我的页面,并通过函数获取卷曲请求。
如何将数组传递给函数?
答案 0 :(得分:5)
function whatever ($params) {
$request = 'api url goes here';
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
print_r($response);
}
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => 'test@test.com',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => 'from@from.com'
);
whatever($params);
分配给变量的数组与分配给变量的任何其他类型的值没有什么不同。
答案 1 :(得分:1)
function_name($params);
和功能
extract($params);
(只有当你确定自己知道自己在做什么时才这样做)