1

I am trying to write a PHP script that post username and password as JSON object to a server and in response get a JSON object that contains UserId and token in JSON.

Following is my code

if (isset($_GET["user"]) && isset($_GET["key"])){

$username = $_GET["user"];
$pass = $_GET["key"];

$data = array("username" => $username, "password" => $pass);                                                                                   
$data_string = json_encode($data);              
echo $data_string;

$ch = curl_init('https://sce15.appspot.com/api/g_usrs/signin');                                                                                                                                           
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));                                                                                                                   

$response = curl_exec($ch);

curl_close($ch);

$usr = json_decode($response, true);
echo $usr;

}
else{

    echo "Oops Something went wrong: Error 1";
}

But I am not getting anything in output, my browser window is white blank. I am not able to figure out the mistake. I am able to get JSON object when using javascript for the same URL, but I need to do it with PHP.

Any help is appreciated. Thanks

10
  • This is going to be impossible to answer without knowing the web service you're posting to, and how it responds.
    – Mitya
    Commented Aug 23, 2017 at 14:29
  • the webserver gives me the JSON object with some values. The problem is not with server, i have tested the same using javascript script Commented Aug 23, 2017 at 14:32
  • Maybe the JSON is bad?
    – ryantxr
    Commented Aug 23, 2017 at 14:33
  • JSON is perfectly formed, i have tested it, Do you think that the code is error free ? Commented Aug 23, 2017 at 14:33
  • you have a totally blank screen? so even echo $data_string; doesn't print anything? Commented Aug 23, 2017 at 14:58

1 Answer 1

1

I finally pointed out the problem after 2 days of head banging on google. It was the SSL certificate error, CURL was not able to verify the SSL certificate.

I got to knew that after checking curl errors print_r(curl_error($ch));

And then by using the curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); i was able to disable SSL certificate security check. Although it made my connection insecure but i think that's not important in my case.

Thanks Everyone

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.