Skip to content

Latest commit

 

History

History
117 lines (94 loc) · 2.14 KB

php.md

File metadata and controls

117 lines (94 loc) · 2.14 KB

Install dependencies through composer

$ composer install

Run migration for laravel

$ php artisan migrate

Start a local server

$ php -S localhost:8000

Posting JSON using cURL

<?php

class SomeService {
	public function authorize($url, $data) {

		$defaults = array(
			CURLOPT_URL => $url,
			CURLOPT_POST => 1,
			CURLOPT_POSTFIELDS => json_encode($data),
			CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
			CURLOPT_RETURNTRANSFER => 1
		);

		$ch = curl_init();
		curl_setopt_array($ch, $defaults);

		if (!$result = curl_exec($ch) ) {
			return curl_error($ch);
		}

		if (!curl_errno($ch)) {
			switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
				case 200:
				break;
				default:
				return 'Unexpected HTTP code: ' . $http_code . '. Error: ' . $result;
			}
		}
		return json_decode($result);
	}

}
$url = 'http://localhost:3000/';
$auth = new SomeService();

$jsonData = array(
	'election' => 1,
	'name' => 'test'
);

$output = $auth->authorize($url, $jsonData);
// var_dump($output);
// print_r($output);
// echo $output->name;
// die();

Posting Form Data with cURL

<?php

class SomeService {
	public function authorize($url, $data) {

		$defaults = array(
			CURLOPT_URL => $url,
			CURLOPT_POST => 1,
			CURLOPT_POSTFIELDS => http_build_query($data),
			CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
			CURLOPT_RETURNTRANSFER => 1
		);

		$ch = curl_init();
		curl_setopt_array($ch, $defaults);

		if (!$result = curl_exec($ch) ) {
			return curl_error($ch);
		}

		if (!curl_errno($ch)) {
			switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
				case 200:
				break;
				default:
				return 'Unexpected HTTP code: ' . $http_code . '. Error: ' . $result;
			}
		}
		return json_decode($result);
	}
}
$url = 'http://localhost:5000/connect/token';
$auth = new SomeService();

$jsonData = array(
	'grant_type' => 'client_credentials',
	'scope' => 'api',
	'client_id' => 'client',
	'client_secret' => 'secret'
);

$output = $auth->authorize($url, $jsonData);
var_dump($output);
// print_r($output);
// echo $output->name;
// die();