1

I have the following code:

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => '[email protected]',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => '[email protected]'
);  

/* 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 */

Basically I want to put the above code between the comments into a function, so that I can set the params and then do the following:

sendthat($params);

This is to simply clean up my page, and have the curl request grabbing the array via a function.

How can I pass the array to a function?

3
  • 4
    Like you pass any other argument/value. Commented Nov 7, 2011 at 14:14
  • UH? Well, the same way you pass a variable... Commented Nov 7, 2011 at 14:15
  • 2
    your code already passes an array to a function (second curl_setopt call). Commented Nov 7, 2011 at 14:20

2 Answers 2

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'        => '[email protected]',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => '[email protected]'
);



whatever($params);

An array assigned to a variable is no different than any other type of value assigned to a variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the only non sarcastic reply.
1
function_name($params);

and in function

extract($params);

(do it only when you're sure you know what you're doing)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.