Skip to content

Instantly share code, notes, and snippets.

@randolphpebenito
Last active September 21, 2015 10:34
Show Gist options
  • Save randolphpebenito/c5ad47b17227a97b75ce to your computer and use it in GitHub Desktop.
Save randolphpebenito/c5ad47b17227a97b75ce to your computer and use it in GitHub Desktop.
HTTP POST JSON Data using PHP Curl (Login API)
<?php
#FIXME: Hardcoded url
$url = 'http://kantokras.ph/api/account/login/';
#Assumption: The params below have already been verified.
$topup_data = array("appCode" => "KantoKrasKing",
"msisdn" => "639178680114",
"password" => "abc123");
#JSON encode the params
$topup_str = json_encode($topup_data);
#Curl init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($topup_str)
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topup_str);
$result = curl_exec($ch);
#Ensure to close curl
curl_close($ch);
#For verbosity
print_r($result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment