0

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)

4
  • Try echo curl_getinfo($ch, CURLINFO_HTTP_CODE); after curl_exec and see what it says. Commented Nov 26, 2013 at 10:35
  • did you try my solution? @rani Commented Nov 26, 2013 at 10:56
  • Wayne Whitty - I did it, and it says "0"... not sure at all what this means. Commented Nov 27, 2013 at 12:03
  • Developer - I did try it, but it didn't work, I still got blank response Commented Nov 27, 2013 at 12:04

1 Answer 1

1

Try this @rani now you can get response.

    $url = 'URL/api/mylogin';
    $ch = curl_init($url);

 //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // RETURN THE CONTENTS OF THE CALL
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
 //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
 curl_setopt($ch, CURLOPT_USERPWD, 'myLogin'.':'.'myPassword'); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

 $response = curl_exec($ch);
 curl_close($ch);
 echo $response;

or pass key as a password in CURLOPT_USERPWD.

Sign up to request clarification or add additional context in comments.

3 Comments

This returns a blank page as well. Btw I'm actually not sure that the server I'm querying supports "CURLOPT_USERPWD".
@Rani..ohh then make sure first?
Can't the username and password be passed at regular key:value ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.