Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sgupta1979's avatar

Service now API with Laravel

I create servicenow API to retrieve data. Here is the code :

Response I am getting as below

Illuminate\Http\JsonResponse {#309 ▼ // app\Http\Controllers\RhoneController.php:37
  +headers: 
Symfony\Component\HttpFoundation
\
ResponseHeaderBag
 {#305 ▼
    #headers: array:3 [▶]
    #cacheControl: []
    #computedCacheControl: array:2 [▶]
    #cookies: []
    #headerNames: array:3 [▶]
  }
  #content: "{}"
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
  -sentHeaders: ? array
  #data: "{}"
  #callback: null
  #encodingOptions: 0
  +original: ArrayObject {#317 ▶}
  +exception: null
}

No data avaialble. Please guide

0 likes
3 replies
LaryAI's avatar
Level 58

To integrate with the ServiceNow API using Laravel, you can use Laravel's HTTP client, which is built on top of Guzzle. Below is a basic example of how you can set up a request to the ServiceNow API to retrieve data.

First, ensure you have the necessary credentials and endpoint details from your ServiceNow instance.

Here's a step-by-step solution:

  1. Install Guzzle (if not already installed):

    Laravel 7 and above come with Guzzle as part of the HTTP client, so you might not need to install it separately. However, if you're using an older version, you can install it via Composer:

    composer require guzzlehttp/guzzle
    
  2. Set up environment variables:

    Add your ServiceNow credentials and endpoint to your .env file:

    SERVICENOW_INSTANCE_URL=https://your-instance.service-now.com
    SERVICENOW_USERNAME=your_username
    SERVICENOW_PASSWORD=your_password
    
  3. Create a Service class to handle the API requests:

    You can create a service class in your Laravel application to encapsulate the logic for interacting with the ServiceNow API.

    namespace App\Services;
    
    use Illuminate\Support\Facades\Http;
    
    class ServiceNowApiService
    {
        protected $baseUrl;
        protected $username;
        protected $password;
    
        public function __construct()
        {
            $this->baseUrl = config('services.servicenow.url');
            $this->username = config('services.servicenow.username');
            $this->password = config('services.servicenow.password');
        }
    
        public function getData($endpoint)
        {
            $response = Http::withBasicAuth($this->username, $this->password)
                ->get("{$this->baseUrl}/api/now/table/{$endpoint}");
    
            if ($response->successful()) {
                return $response->json();
            }
    
            return null;
        }
    }
    
  4. Configure the service in config/services.php:

    Add the ServiceNow configuration to your config/services.php file:

    'servicenow' => [
        'url' => env('SERVICENOW_INSTANCE_URL'),
        'username' => env('SERVICENOW_USERNAME'),
        'password' => env('SERVICENOW_PASSWORD'),
    ],
    
  5. Use the service in a controller:

    Inject the ServiceNowApiService into your controller and use it to fetch data.

    namespace App\Http\Controllers;
    
    use App\Services\ServiceNowApiService;
    
    class ServiceNowController extends Controller
    {
        protected $serviceNowApiService;
    
        public function __construct(ServiceNowApiService $serviceNowApiService)
        {
            $this->serviceNowApiService = $serviceNowApiService;
        }
    
        public function index()
        {
            $data = $this->serviceNowApiService->getData('incident'); // Example endpoint
    
            return view('servicenow.index', compact('data'));
        }
    }
    
  6. Create a view to display the data:

    Finally, create a Blade view to display the data retrieved from the ServiceNow API.

    <!-- resources/views/servicenow/index.blade.php -->
    
    <h1>ServiceNow Data</h1>
    
    @if($data)
        <ul>
            @foreach($data['result'] as $item)
                <li>{{ $item['number'] }} - {{ $item['short_description'] }}</li>
            @endforeach
        </ul>
    @else
        <p>No data found.</p>
    @endif
    

This setup provides a basic structure for interacting with the ServiceNow API using Laravel. You can expand upon this by adding error handling, logging, and more complex data processing as needed.

Please or to participate in this conversation.