Wednesday, February 2, 2022

[SOLVED] PHP CURL returns Invalid Parameters but works with FORM method

Issue

I am trying to create an API. The problem is that it's working when use a simple form to post the data to the URL but it doesn't work with my CURL code. With CURL it returns Invalid Parameters message.

Working Form Method

<form method="post" action="https://hrtaskapi.dhwaniris.in/index.php/api/user/login">
  <input type="text" name="username" value="1412" class="form-control">
  <input type="text" name="password" value="142487" class="form-control">
  <input type="submit" name="submit" value="Login">
</form>

Working Response

{"success":true,"status":200,"message":"Login successfull.","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEwMTEiLCJ1bmlxdWVfdXNlcl9pZCI6IjEwMTEiLCJuYW1lIjoiQmhhZ3llc2ggQ29vcmRpbmF0b3IiLCJ1c2VybmFtZSI6IjE0MTIiLCJtb2JpbGUiOiI4NDg4MDc0NjEyIiwibGV2ZWxfaWQiOiI1IiwicGFzc3dvcmRfcmVzZXRfdG9rZW4iOiIxNDI0ODciLCJlbWFpbCI6ImJoYWd5ZXNoQGRod2FuaXJpcy5jb20iLCJhdXRoX2tleSI6ImQ0MWQ4Y2Q5OGYwMGIyMDRlOTgwMDk5OGVjZjg0MjdlIiwic3RhdHVzIjoiMSIsImNyZWF0ZWRfYXQiOiIwMDAwLTAwLTAwIDAwOjAwOjAwIiwidXBkYXRlZF9hdCI6IjIwMTktMTAtMTUgMDc6MDY6MTIiLCJjaGFuZ2VkX3Bhc3N3b3JkX29uY2UiOm51bGwsInVzZXJfZXJyb3JfZnJlcXVlbmN5XzEiOm51bGwsInVzZXJfZXJyb3JfZnJlcXVlbmN5XzIiOiIzIiwidXNlcl9sZXZlbCI6IkNsdXN0ZXIgU3VwZXJ2aXNvciIsImxvZ2luX2lkIjoxMDM2fQ.8MU6ENYiCzlxEKxoFbd3C_oQga_-y9jsXrag8zebpFw","login_id":1036,"last_login":1614015416,"timestamp":1614017666}

Not Working CURL Method

$url = "https://hrtaskapi.dhwaniris.in/index.php/api/user/login";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=1412&password=142487');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
echo "<pre>";
print_r($result);

Response From CURL

stdClass Object
(
    [success] => 
    [status] => 400
    [message] => Invalid Parameters
)

What seems to be the issue here? What's wrong with my CURL code that's preventing it from proper execution? I have searched for hours and couldn't find out the mistake. Also, I am new to CURL and API.

UPDATE

Replacing Content-Type:application/json with Content-Type:application/x-www-form-urlencoded returns the following.

stdClass Object
(
    [name] => PHP Notice
    [message] => Undefined index: HTTP_USER_AGENT
    [code] => 8
    [type] => yii\base\ErrorException
    [file] => /var/www/html/projects/census_mobile_api/backend/modules/api/controllers/UserController.php
    [line] => 61
    [stack-trace] => Array
        (
            [0] => #0 /var/www/html/projects/census_mobile_api/backend/modules/api/controllers/UserController.php(61): yii\base\ErrorHandler->handleError(8, 'Undefined index...', '/var/www/html/p...', 61, Array)
            [1] => #1 [internal function]: backend\modules\api\controllers\UserController->actionLogin()
            [2] => #2 /var/www/html/projects/census_mobile_api/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
            [3] => #3 /var/www/html/projects/census_mobile_api/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
            [4] => #4 /var/www/html/projects/census_mobile_api/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('login', Array)
            [5] => #5 /var/www/html/projects/census_mobile_api/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('api/user/login', Array)
            [6] => #6 /var/www/html/projects/census_mobile_api/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
            [7] => #7 /var/www/html/projects/census_mobile_api/backend/web/index.php(18): yii\base\Application->run()
            [8] => #8 {main}
        )

)

Solution

As I suggested use array as Postfields and set a Useragent.

$url = "https://hrtaskapi.dhwaniris.in/index.php/api/user/login";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['username' => '1412', 'password' => '142487']);
curl_setopt($ch, CURLOPT_USERAGENT, '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36');
$result = curl_exec($ch);
curl_close($ch);

var_dump(json_decode($result));

Result

class stdClass#2 (7) {
  public $success =>
  bool(true)
  public $status =>
  int(200)
  public $message =>
  string(18) "Login successfull."
  public $token =>
  string(687) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEwMTEiLCJ1bmlxdWVfdXNlcl9pZCI6IjEwMTEiLCJuYW1lIjoiQmhhZ3llc2ggQ29vcmRpbmF0b3IiLCJ1c2VybmFtZSI6IjE0MTIiLCJtb2JpbGUiOiI4NDg4MDc0NjEyIiwibGV2ZWxfaWQiOiI1IiwicGFzc3dvcmRfcmVzZXRfdG9rZW4iOiIxNDI0ODciLCJlbWFpbCI6ImJoYWd5ZXNoQGRod2FuaXJpcy5jb20iLCJhdXRoX2tleSI6ImQ0MWQ4Y2Q5OGYwMGIyMDRlOTgwMDk5OGVjZjg0MjdlIiwic3RhdHVzIjoiMSIsImNyZWF0ZWRfYXQiOiIwMDAwLTAwLTAwIDAwOjAwOjAwIiwidXBkYXRlZF9hdCI6IjIwMTktMTAtMTUgMDc6MDY6MTIiLCJjaGFuZ2VkX3Bhc3N3b3JkX29uY2UiOm51bGwsInVzZXJfZXJyb3J"...
  public $login_id =>
  int(1042)
  public $last_login =>
  int(1614026428)
  public $timestamp =>
  int(1614026465)
}


Answered By - Markus Zeller
Answer Checked By - Timothy Miller (WPSolving Admin)