0

Basically I have this AJAX code to POST data and retrieve html code as the result.

$.ajax({
    type: "POST",
    data: {key:'237644',lc:'',lk:'',value:'8816721951456478'},
    url: "https://link/to/server",
    success: function(r) {
        $("#results").html(r);
    }
});

I want to achieve the same result using PHP cURL approach.
Here is my current cURL code I've work for few hour

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://link/to/server");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,'"key"="237644","lc"="","lk"="","val"="8816721951456478"');
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
    echo $error_msg;
}
echo htmlspecialchars($server_output);
curl_close ($ch);

I've tried using array too on the CURLOPT_POSTFIELD

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('key'=>'237644', 'lc'=>'', 'lk'=>'', 'value'=>'8816721951456478')));

But with no luck, the cURL still not get the result back.
Is there something I did wrong or I missed in my code?

Also based on my research, cURL can accomplish what AJAX is capable of (in my case), instead it run on the server side.
Let me know if my thoughts is wrong before it goes too far.

Any help would be appreciate.

Vosa
  • 1
  • 1
  • 1
    You don't check for curl errors. If you're lucky the error message will give you a hint as to what the problem is. See: https://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php – KIKO Software Oct 03 '20 at 18:18
  • @KIKOSoftware I added the curl error like your suggestion, and there is no error code or something returned – Vosa Oct 03 '20 at 19:44

0 Answers0