Column Operations
Wednesday, 17 January 2024
Friday, 5 January 2024
PHP Cheating Sheet
USort and Heap
class Solution {
/**
* Leetcode 253
* @param Integer[][] $intervals
* @return Integer
*/
function minMeetingRooms($intervals) {
usort($intervals, function($a, $b) {
return $a[0] > $b[0];
});
$heap = new SplMinHeap();
$ans = 1;
$heap->insert($intervals[0][1]);
for ($i=1; $i<count($intervals); $i++) {
$top = $heap->top();
if ($top <= $intervals[$i][0]) {
$heap->extract();
} else {
$ans++;
}
$heap->insert($intervals[$i][1]);
}
return $ans;
}
}
__invoke() magic method
The __invoke() method is called when a script tries to call an object as a function.
<?php
class CallableClass
{
public function __invoke($x)
{
var_dump($x);
}
}
$obj = new CallableClass;
//will do var_dump (int 5)
$obj(5);
//bool true
var_dump(is_callable($obj));
::class
SomeClass::class will return the fully qualified name of SomeClass including the namespace.
use WW\PayAPI\Middlewares\AuthUsers;
//same as $magic = "WW\PayAPI\Middlewares\AuthUsers";
$magic = AuthUsers::class;
List extensions installed
php -m
GuzzleHttp\Client API Call
<?php
//get.php
//need to install library ./composer.phar require guzzlehttp/guzzle 7.5.0
//usage: php get.php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$url = 'https://bin-lookup.test-api.io/v1/search/519396****8565';
// Make a GET request
$response = $client->request('GET', $url, [
'headers' => [
'Accept' => 'application/json',
'x-api-key' => 'ksjddnd24Sj65vUhyqqqqwww'
],
// You can pass query parameters as well
'query' => [
'param1' => 'value1',
'param2' => 'value2'
]
]);
// Get the response status code
$statusCode = $response->getStatusCode();
echo "Status Code: $statusCode\n";
$body = $response->getBody();
// Convert JSON response to associative array
$data = json_decode($body, true);
// Print the response data
print_r($data);
<?php
//post.php
/**
* usage: php post.php
*/
require 'vendor/autoload.php';
$client = new \GuzzleHttp\Client();
$url = "http://local-support.myapitest.com/rest/v1/orders/purchase-lookup";
$response = $client->post($url, [
'headers' => [
'Content-Type' => 'application/json',
'x-api-key' => 'whasjsjsjsjjsjjsjsuuuu|4'
],
//post body sent will be json encoded string
'json' => [
'email' => 'mytest@testp.com'
]
]);
$body = $response->getBody();
$ans = json_decode($body, true);
var_dump($ans);
$statusCode = $response->getStatusCode();
echo "Status Code: $statusCode\n";
Subscribe to:
Posts (Atom)