MySQL_Narb Posted August 18, 2012 Share Posted August 18, 2012 $data = http_build_query(array('action' => 'vote', 'id' => $id, 'ajax_module' => 'tracking', 'module_type' => 'public', 'dir' => '1')); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_URL, 'http://www.REMOVED.com/ajax.php'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); Is this how I can send POST data, but change the HTTP headers in the request to: Content-Type: application/json Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/267254-sending-curl-post-data-as-content-type-applicationjson/ Share on other sites More sharing options...
requinix Posted August 18, 2012 Share Posted August 18, 2012 $data = http_build_query(array('action' => 'vote', 'id' => $id, 'ajax_module' => 'tracking', 'module_type' => 'public', 'dir' => '1')); That's not JSON. Have you first changed it to send JSON data? Quote Link to comment https://forums.phpfreaks.com/topic/267254-sending-curl-post-data-as-content-type-applicationjson/#findComment-1370381 Share on other sites More sharing options...
MySQL_Narb Posted August 18, 2012 Author Share Posted August 18, 2012 $data = http_build_query(array('action' => 'vote', 'id' => $id, 'ajax_module' => 'tracking', 'module_type' => 'public', 'dir' => '1')); That's not JSON. Have you first changed it to send JSON data? I did have the array wrapped in json_encode(); but I'm pretty sure that didn't work (http_build_query returned an error about accepting only objects and arrays). Quote Link to comment https://forums.phpfreaks.com/topic/267254-sending-curl-post-data-as-content-type-applicationjson/#findComment-1370442 Share on other sites More sharing options...
requinix Posted August 18, 2012 Share Posted August 18, 2012 Right. You're supposed to use json_encode() instead of http_build_query(). Quote Link to comment https://forums.phpfreaks.com/topic/267254-sending-curl-post-data-as-content-type-applicationjson/#findComment-1370452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.