ranienter Posted November 26, 2013 Share Posted November 26, 2013 Hey everyone, I'm trying to send a JSON array to an API via HTTP POST, get a response and print it. I tried using cURL to do so, but it doesn't seem to work. I simply get zero response, a blank page. My request: <?php $data = array( "login" => "myLogin", "password" => "myPassword", "id" => "12345", "tag" => "test" ); $json_data = json_encode($data); $ch = curl_init('URL/api/mylogin'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($json_data)) ); $output = curl_exec($ch); $result = json_decode($output); echo $result; ?> The response I should be getting: {"status": 200, "message": "OK", "login_key": "abcdefh532h235yh"} any idea why I'm not getting any response? (this works ok when I manually test it using a test REST client) Thanks, Rani Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/ Share on other sites More sharing options...
requinix Posted November 26, 2013 Share Posted November 26, 2013 What's the raw $output you got? Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/#findComment-1460225 Share on other sites More sharing options...
ranienter Posted November 27, 2013 Author Share Posted November 27, 2013 I got nothing, a blank page Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/#findComment-1460326 Share on other sites More sharing options...
requinix Posted November 27, 2013 Share Posted November 27, 2013 Any errors? What does your code look like when you var_dump() the $output and the $result, and exactly what output do you get (because you will get something). Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/#findComment-1460411 Share on other sites More sharing options...
bashy Posted November 27, 2013 Share Posted November 27, 2013 Why not turn on header info? curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); or curl_setopt($ch, CURLOPT_HEADER, TRUE); Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/#findComment-1460418 Share on other sites More sharing options...
silkfire Posted November 29, 2013 Share Posted November 29, 2013 What does this give you: var_dump(curl_getinfo($ch), curl_error($ch)); Link to comment https://forums.phpfreaks.com/topic/284300-http-post-a-json-array-using-phpcurl/#findComment-1460609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.