How to convert cURL to PHP cURL
Couple of nits to pick in your code aside from the obvious typo of CURLOPT_POST
. You use filter_var
but carry on if the filter fails; you should check for a false value. You should not be using urldecode
on the URL, the whole idea of http_build_query
is that it does the URL encoding for you. This one's a personal preference but I find curl_setopt_array
much easier to work with. And, you should be checking if your result is valid JSON and use proper headers when sending the response back to jQuery.
header("Content-Type: application/json");$email = filter_var($_POST["Email"], FILTER_SANITIZE_EMAIL);if (!$email) { header("HTTP/1.1 500 Server Error"); $res = ["result"=>false, "message"=>"Invalid email"]; echo json_encode($res); exit;}$fields = ['domain' => $email];$url = "https://mailcheck.p.mashape.com/?" . http_build_query($fields);$ch = curl_init($url);curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'X-Mashape-Key: f123456879', 'Accept: application/json' ]]);$server_output = curl_exec($ch);curl_close ($ch);if (json_decode($server_output)) { echo $server_output;} else { header("HTTP/1.1 500 Server Error"); $res = ["result"=>false, "message"=>"Invalid response from server: $server_output"]; echo json_encode($res);}