PHP Integration

Generate QR codes in PHP using cURL or Guzzle HTTP client.

Using cURL (No Dependencies)

<?php

function generateQR($data, $options = []) {
    $apiKey = getenv('QR_API_KEY');
    
    $params = array_merge(['data' => $data], $options);
    $url = 'https://qrcodeapi.io/api/generate?' . http_build_query($params);
    
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . $apiKey
        ]
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($httpCode !== 200) {
        throw new Exception('QR generation failed: ' . $httpCode);
    }
    
    return $response;
}

// Usage
$qrImage = generateQR('https://example.com', ['size' => 400]);
file_put_contents('qr.png', $qrImage);
?>

Using Guzzle

<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;

class QRCodeAPI {
    private $client;
    private $apiKey;
    
    public function __construct($apiKey) {
        $this->apiKey = $apiKey;
        $this->client = new Client([
            'base_uri' => 'https://qrcodeapi.io',
            'headers' => [
                'Authorization' => 'Bearer ' . $apiKey
            ]
        ]);
    }
    
    public function generate($data, $options = []) {
        $response = $this->client->get('/api/generate', [
            'query' => array_merge(['data' => $data], $options)
        ]);
        
        return $response->getBody()->getContents();
    }
}

// Usage
$qr = new QRCodeAPI(getenv('QR_API_KEY'));
$image = $qr->generate('https://example.com', [
    'size' => 400,
    'color' => '4F46E5'
]);
file_put_contents('qr.png', $image);
?>

Laravel Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class QRController extends Controller
{
    public function generate(Request $request)
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . config('services.qrcode.key')
        ])->get('https://qrcodeapi.io/api/generate', [
            'data' => $request->input('data'),
            'size' => $request->input('size', 300)
        ]);
        
        return response($response->body())
            ->header('Content-Type', 'image/png');
    }
}
?>

WordPress Shortcode

<?php

function qrcode_shortcode($atts) {
    $atts = shortcode_atts([
        'data' => '',
        'size' => 200,
        'alt' => 'QR Code'
    ], $atts);
    
    if (empty($atts['data'])) {
        return '';
    }
    
    $url = 'https://qrcodeapi.io/api/generate?' . http_build_query([
        'data' => $atts['data'],
        'size' => $atts['size']
    ]);
    
    // Note: For authenticated requests, you'd need server-side generation
    return sprintf(
        '<img src="%s" alt="%s" width="%d" height="%d" />',
        esc_url($url),
        esc_attr($atts['alt']),
        intval($atts['size']),
        intval($atts['size'])
    );
}
add_shortcode('qrcode', 'qrcode_shortcode');

// Usage: [qrcode data="https://example.com" size="200"]
?>

Batch Generation

<?php

function generateBatch($items) {
    $apiKey = getenv('QR_API_KEY');
    
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => 'https://qrcodeapi.io/api/batch',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode([
            'items' => $items,
            'output' => 'json'
        ]),
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json'
        ]
    ]);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

// Usage
$results = generateBatch([
    ['data' => 'https://example1.com'],
    ['data' => 'https://example2.com'],
    ['data' => 'https://example3.com']
]);
?>

Get Your API Key

Get Free API Key