A
A
Alexander2018-03-03 19:32:08
PHP
Alexander, 2018-03-03 19:32:08

How to send a post request from php function?

In php, I form fields for the form through foreach, i.e.: input name=email value= blabla ...
Now I need to send each form with a post request to a specific url, i.e. as it usually happens on html: form action:url
But you need to send it not from the html page, but from the php function.
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
John Doe, 2018-03-03
@noobwpjs

<?php
function post($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close ($ch);

    return $output;
}

echo post("https://google.com", [
    'foo' => 'bar'
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question