PHP code example of sanjokdangol / apicall

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']);
    }
}

ApiCall::actingAs($user)->get('/api/posts');
ApiCall::actingAs($user, ['read', 'write'])->post('/api/posts', $data);

ApiCall::asUser($user)->get('/api/posts');

ApiCall::withToken($token)->get('/api/posts');

$posts = ApiCall::actingAs($user)->get('/api/posts');
$post = ApiCall::actingAs($user)->get('/api/posts/1');
$filtered = ApiCall::actingAs($user)->get('/api/posts', ['status' => 'published']);

$post = ApiCall::actingAs($user)->post('/api/posts', [
    'title' => 'New Post',
    'body' => 'Content'
]);

$updated = ApiCall::actingAs($user)->put('/api/posts/1', [
    'title' => 'Updated Title'
]);

$updated = ApiCall::actingAs($user)->patch('/api/posts/1', [
    'status' => 'published'
]);

$result = ApiCall::actingAs($user)->delete('/api/posts/1');

$data = ApiCall::actingAs($user)->clean()->get('/api/posts');
// Returns: array or object

$response = ApiCall::actingAs($user)->response()->get('/api/posts');
// Returns: Illuminate\Http\Response
// Access: $response->getStatusCode(), $response->getContent(), etc.

ApiCall::actingAs($user)->dd()->post('/api/posts', $data);
// Dumps request/response details and stops execution

// config/apicall.php

return [
    // Enable/disable API call logging
    'log' => env('APICALL_LOG', false),
    
    // Log channel
    'log_channel' => env('APICALL_LOG_CHANNEL', 'single'),
    
    // Token name for authentication
    'token_name' => env('APICALL_TOKEN_NAME', 'ApiCall'),
    
    // Default scopes
    'default_scopes' => [],
];

// 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');
}

'providers' => [
    // ...
    Sanjokdangol\ApiCall\Providers\ApiCallServiceProvider::class,
],

'aliases' => [
    // ...
    'ApiCall' => Sanjokdangol\ApiCall\Facades\ApiCall::class,
],
bash
php artisan package:discover --ansi
bash
php artisan vendor:publish --provider="Sanjokdangol\ApiCall\Providers\ApiCallServiceProvider" --tag="apicall-config"
bash
php artisan vendor:publish --provider="Sanjokdangol\ApiCall\Providers\ApiCallServiceProvider" --tag="apicall-config"

packages/sanjokdangol/apicall/
├── src/
│   ├── Facades/
│   │   └── ApiCall.php              # Facade for easy access
│   ├── Providers/
│   │   └── ApiCallServiceProvider.php # Service provider
│   └── Services/
│       └── ApiCall.php              # Core service class
├── config/
│   └── apicall.php                  # Configuration file
├── database/                        # Database files (if needed)
├── resources/                       # Resources (if needed)
├── tests/
│   └── Feature/
│       └── ApiCallTest.php          # Package tests
├── composer.json                    # Package dependencies
├── LICENSE.md                       # MIT License
├── README.md                        # This file
└── .gitignore                       # Git ignore rules
bash
php artisan vendor:publish --provider="Sanjokdangol\ApiCall\Providers\ApiCallServiceProvider" --tag="apicall-docs"
bash
php artisan package:discover --ansi
composer dump-autoload
php artisan cache:clear