1. Go to this page and download the library: Download sanjokdangol/apicall library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sanjokdangol / apicall example snippets
use Sanjokdangol\ApiCall\Facades\ApiCall;
use App\Models\User;
$user = User::find(1);
// Simple GET request
$posts = ApiCall::actingAs($user)
->clean()
->get('/api/posts');
// POST with data
$response = ApiCall::actingAs($user)
->post('/api/posts', [
'title' => 'My Post',
'body' => 'Content here'
]);
// With debugging
$response = ApiCall::actingAs($user)
->dd()
->put('/api/users/1', ['name' => 'Updated']);
namespace App\Http\Controllers;
use ApiCall;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$user = auth()->user();
$post = ApiCall::actingAs($user)
->post('/api/posts', $request->validated());
return response()->json($post, 201);
}
}
namespace App\Services;
use Sanjokdangol\ApiCall\Facades\ApiCall;
class NotificationService
{
public function sendNotification($user, $data)
{
return ApiCall::actingAs($user)
->post('/api/notifications', $data);
}
}
namespace App\Jobs;
use ApiCall;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class SyncUserDataJob implements ShouldQueue
{
use Queueable;
protected $user;
public function __construct($user)
{
$this->user = $user;
}
public function handle()
{
$data = ApiCall::actingAs($this->user)
->clean()
->get('/api/user-data');
// Process data...
}
}
namespace Tests\Feature;
use Tests\TestCase;
use ApiCall;
use App\Models\User;
class PostApiTest extends TestCase
{
public function test_user_can_create_post()
{
$user = User::factory()->create();
$response = ApiCall::actingAs($user)
->post('/api/posts', [
'title' => 'Test Post',
'body' => 'Test content'
]);
$this->assertArrayHasKey('id', $response);
$this->assertEquals('Test Post', $response['title']);
}
}
// Without leaving the app, call your own API endpoints
$userData = ApiCall::actingAs($user)->get('/api/users/profile');
// Test as if making HTTP requests, but in-process
$response = ApiCall::actingAs($user)->post('/api/posts', $data);
// Execute API calls in queued jobs with proper user context
dispatch(new ProcessUserDataJob($user));
// Inside the job
ApiCall::actingAs($this->user)->post('/api/data/process', $data);
// Call APIs from other packages/modules
$result = ApiCall::actingAs($user)->get('/api/dashboard/stats');
// Handle events by making API calls
public function handle(UserCreated $event)
{
ApiCall::actingAs($event->user)->post('/api/onboarding/start');
}