Tag Archives: PHP

Arguments date in PHP

PHP

	if ( isset( $argv ) && isset($argv[1])) {
		$d = DateTime::createFromFormat("Y-m-d", $argv[1]);
		$date_errors = DateTime::getLastErrors();
		
		if ($date_errors['warning_count'] + $date_errors['error_count'] > 0) {
			die();
		}else{
			$obs_date = $argv[1];
		}
	}else{
		$obs_date = date('Y-m-d');
	}

CMD
php index.php 2019-09-10

Post JSON to web api in PHP

[Action] POST JSON format

{
    "command": "login",
    "params": {
        "username": "username",
        "password": "password"
    }
}

[Action] Return POST result

{
    "success": true
}

index.php

$data = array(
  'command' => 'login',
  'params' => array(
        'username' => 'username',
        'password' => 'password'    
  )
);
json_encode( $data );

$api_url = 'https://study.ar-leung.net';
$ch = curl_init( $api_url );

# Setup request to send json via POST.
$payload = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

# Send request.
$result = curl_exec($ch);
curl_close($ch);

# Print response.
echo "<pre>$result</pre>";

$json_data = json_decode($result);
echo $json_data->success;