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! 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? 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). 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(). 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
Archived
This topic is now archived and is closed to further replies.