0

I'm trying to create a query string from POST data that I already have set as the variables $email and $ip. It needs to be application/x-www-form-urlencoded

could I use http_build_query? A little rusty here. I just need help pulling the variables and adding them to the string to POST.

Here is the section I'm working on. (Just using this for testing to see responses)

    function coSponsorPush2($email) {

        global $ip;
        global $email;

        $curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => '#',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => http_build_query(['email' => $email, 'ip_address' => $ip]),
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$resp = curl_exec($curl);
curl_close($curl);

}

Thank you!

  • From the documentation: "This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data." – miken32 Apr 14 '22 at 22:59
  • I might be overthinking this. I know I need it to be para1=val1&para2=val2, But how do I replace the values with my variables without switching to multipart/form-data? – KirbyBell561 Apr 14 '22 at 23:38
  • As you suggest, `http_build_query` is the appropriate function to use to build the string – miken32 Apr 15 '22 at 00:28
  • So like `$post = http_build_query([“email” => $email, “ip” => $ip]);` – miken32 Apr 15 '22 at 00:35
  • Thanks Mike, I got it to work with this. However, when placed inside a function, it doesn't work. Edited the above code with the function. – KirbyBell561 Apr 15 '22 at 14:02
  • You’ll want to look at how to pass parameters to functions, and when is an appropriate time to use thé `global` declaration. (Hint: it’s never) – miken32 Apr 15 '22 at 14:40

0 Answers0