0

I need your help to check whats wrong with my code. I had tried to post a json data from postman and it returnen a correct response. But the following code always returns a wrong response.

<?php
$data_login = array('email'=>'[email protected]','password'=>'hahaha','confirmation_password'=>'hahaha');

$api_data = json_encode($data_login);
$api_url = 'http://dev.badr.co.id/freedom/auth/register';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $api_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);

echo $result;

wrong response :

{"success":false,"message":"1000: Not a valid request"}

correct response :

{
    "success": true,
    "message": "user registration success",
    "data": null
}

This return a correct response if i post the data using postman :

correct_response

9
  • Without the details of the API - what the endpoint expects - I doubt whether we'll be much help. Commented Sep 1, 2017 at 9:43
  • It says:- {"success":false,"message":"1000: Not a valid request"} that means you are not using API in it's told way Commented Sep 1, 2017 at 9:44
  • I ran the CLI curl example in the api documentation and got the same response: {"success":false,"message":"1000: Not a valid request"}. I have no idea if this is a working endpoint or not. I can't even work out what Postman or Freedom Php is, from leafing through the website. Commented Sep 1, 2017 at 10:04
  • @kartaterazu can you be a little more explicit, when you say it returns false? Can you var_dump($result) please. Commented Sep 1, 2017 at 10:16
  • @Progrock this is the result {"success":false,"message":"1000: Not a valid request"} Commented Sep 1, 2017 at 10:21

2 Answers 2

1

If curl_exec() is returning false that means that the request is failing somehow.

You can work out how by using the curl_error() function. Call it in between curl_exec() and curl_close() and it will return a string with information on what went wrong with the request.

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

Comments

0

Check the return values of the initializing and executing cURL functions. curl_error() and curl_errno() will contain further information in case of failure:

try {
    $data_login = array('email'=>'[email protected]','password'=>'hahaha','confirmation_password'=>'hahaha');
    $api_data = json_encode($data_login);
    $api_url = 'http://dev.badr.co.id/freedom/auth/register';

    $ch = curl_init();

    if (FALSE === $ch)
        throw new Exception('failed to initialize');

    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $api_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    if (FALSE === $result)
        throw new Exception(curl_error($ch), curl_errno($ch));
} catch(Exception $e) {

    echo sprintf(
        'Curl failed with error #%d: %s',
        $e->getCode(), $e->getMessage());
}

2 Comments

@krishnaraj, rather than proxying the exception through trigger_error, why not just output the curl_error for this demo/example? Though having tested the code, the code doesn't error for me. I get a JSON response.
@kartaterazu Display errors could be turned off in the php.ini or your Apache config file. You can turn it on in the script: error_reporting(E_ALL); ini_set('display_errors', 1); You should see the same messages in the PHP error log.

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.