|
| 1 | +--- |
| 2 | +name: generate-and-scan-barcode-php |
| 3 | +description: Write or update PHP code that uses the Aspose.BarCode Cloud SDK (`Aspose\BarCode\...`; Composer package `aspose/barcode-cloud-php`) to generate, recognize, or scan barcodes through Aspose's cloud REST API. Use this skill whenever the user wants barcode work in PHP, touches files under `submodules/php`, or mentions `GenerateApi`, `RecognizeApi`, `ScanApi`, `Configuration`, `GenerateRequestWrapper`, `GenerateBodyRequestWrapper`, `RecognizeBase64Request`, or `ScanMultipartRequestWrapper`. The PHP SDK has several easy-to-miss idioms, including constructing APIs as `new ...Api(null, $config)`, wrapping every call in a request-wrapper object, treating generate results as `SplFileObject` streams, using public `file_url` values for GET recognize and scan methods, and base64-encoding body payloads yourself. |
| 4 | +--- |
| 5 | + |
| 6 | +# Generate and scan barcode in PHP |
| 7 | + |
| 8 | +Use this skill to write PHP code against the generated Aspose.BarCode Cloud SDK or to maintain the PHP SDK inside `submodules/php`. Most tasks boil down to choosing the right API class, choosing the right request wrapper, and using the correct transport shape for the data you have. |
| 9 | + |
| 10 | +The Composer package name and PHP namespace differ. Install `aspose/barcode-cloud-php`, then import classes from `Aspose\BarCode\...`. |
| 11 | + |
| 12 | +## Quick start |
| 13 | + |
| 14 | +Use these imports in most PHP examples: |
| 15 | + |
| 16 | +```php |
| 17 | +use Aspose\BarCode\Configuration; |
| 18 | +use Aspose\BarCode\GenerateApi; |
| 19 | +use Aspose\BarCode\RecognizeApi; |
| 20 | +use Aspose\BarCode\ScanApi; |
| 21 | +``` |
| 22 | + |
| 23 | +Prefer creating one `Configuration` instance and passing it into each API class: |
| 24 | + |
| 25 | +```php |
| 26 | +$config = new Configuration(); |
| 27 | +$config->setClientId($clientId); |
| 28 | +$config->setClientSecret($clientSecret); |
| 29 | + |
| 30 | +$generateApi = new GenerateApi(null, $config); |
| 31 | +$recognizeApi = new RecognizeApi(null, $config); |
| 32 | +$scanApi = new ScanApi(null, $config); |
| 33 | +``` |
| 34 | + |
| 35 | +If the task edits SDK source, tests, snippets, or generated files in `submodules/php`, read `references/repo-workflow.md`. If the task needs the closest existing example or snippet, read `references/snippet-map.md`. |
| 36 | + |
| 37 | +## Authenticate correctly |
| 38 | + |
| 39 | +Use one of these two patterns: |
| 40 | + |
| 41 | +1. Let the SDK fetch an access token lazily from client credentials. |
| 42 | + |
| 43 | +```php |
| 44 | +$config = new Configuration(); |
| 45 | +$config->setClientId($clientId); |
| 46 | +$config->setClientSecret($clientSecret); |
| 47 | +``` |
| 48 | + |
| 49 | +2. Inject a pre-fetched bearer token. |
| 50 | + |
| 51 | +```php |
| 52 | +$config = new Configuration(); |
| 53 | +$config->setAccessToken($token); |
| 54 | +``` |
| 55 | + |
| 56 | +The runtime defaults are: |
| 57 | + |
| 58 | +- `Host`: `https://api.aspose.cloud` |
| 59 | +- `BasePath`: `/v4.0` |
| 60 | +- `AuthUrl`: `https://id.aspose.cloud/connect/token` |
| 61 | + |
| 62 | +Inside this repo, tests load `tests/Configuration.json` first and then fall back to `TEST_CONFIGURATION_*` environment variables. Snippets usually check `TEST_CONFIGURATION_ACCESS_TOKEN` first and otherwise rely on client credentials. Mirror the surrounding file when editing existing repo code. |
| 63 | + |
| 64 | +## Choose the right API shape |
| 65 | + |
| 66 | +Pick the operation first: |
| 67 | + |
| 68 | +- `GenerateApi`: create a barcode image. |
| 69 | +- `RecognizeApi`: decode one or more known barcode types and optionally tune recognition. |
| 70 | +- `ScanApi`: auto-detect barcode types with the smallest API surface. |
| 71 | + |
| 72 | +Then pick the transport variant based on what the caller has: |
| 73 | + |
| 74 | +- Public internet URL to an image: use `recognize()` or `scan()`. `file_url` must be publicly reachable, not a local path. |
| 75 | +- Local file on disk: use `recognizeMultipart()` or `scanMultipart()` with `new SplFileObject(..., 'rb')`. |
| 76 | +- Raw bytes already in memory: call `base64_encode()` yourself and use `recognizeBase64()` or `scanBase64()`. |
| 77 | +- Simple text plus query parameters for generation: use `generate()`. |
| 78 | +- Structured generate payload: use `generateBody()`. |
| 79 | +- Multipart-form generation: use `generateMultipart()` when the caller explicitly needs that shape. |
| 80 | + |
| 81 | +Key method names: |
| 82 | + |
| 83 | +- `generate` |
| 84 | +- `generateBody` |
| 85 | +- `generateMultipart` |
| 86 | +- `recognize` |
| 87 | +- `recognizeBase64` |
| 88 | +- `recognizeMultipart` |
| 89 | +- `scan` |
| 90 | +- `scanBase64` |
| 91 | +- `scanMultipart` |
| 92 | + |
| 93 | +## Follow the PHP-specific SDK rules |
| 94 | + |
| 95 | +1. Instantiate APIs as `new GenerateApi(null, $config)` and the equivalent `RecognizeApi` or `ScanApi` constructors. The first constructor argument is the optional Guzzle client, not the `Configuration`. |
| 96 | +2. Pass a request-wrapper object for every endpoint. Examples include `GenerateRequestWrapper`, `GenerateBodyRequestWrapper`, `RecognizeMultipartRequestWrapper`, and `ScanBase64RequestWrapper`. |
| 97 | +3. Treat `generate()`, `generateBody()`, and `generateMultipart()` results as `\SplFileObject`. Read them with `getSize()` and `fread()`, save them to disk, or pass them directly into `ScanMultipartRequestWrapper`. |
| 98 | +4. Build base64-body requests in two layers: create `RecognizeBase64Request` or `ScanBase64Request`, then wrap that model in the matching `...RequestWrapper`. |
| 99 | +5. Call `base64_encode()` yourself before `recognizeBase64()` or `scanBase64()`. The SDK does not encode raw bytes for you. |
| 100 | +6. Use one `DecodeBarcodeType` for `RecognizeRequestWrapper` and `RecognizeMultipartRequestWrapper`. Use a list in `RecognizeBase64Request(['barcode_types' => [...]])` when the request body should search multiple types. |
| 101 | +7. Use GET-based recognize and scan methods only for public URLs. For local files, use multipart or base64 instead. |
| 102 | +8. Expect `BarcodeResponseList` from recognize and scan methods. Iterate `getBarcodes()` and read `getBarcodeValue()`, `getType()`, `getRegion()`, and `getChecksum()`. |
| 103 | +9. Let the SDK fetch a token automatically when `AccessToken` is empty but `ClientId` and `ClientSecret` are present. On `401`, the generated clients refresh the token and retry once. |
| 104 | +10. Turn on `setDebug(true)` when request and response logging would help, and use `setUserAgent()` when a custom user agent is required. |
| 105 | +11. Catch `Aspose\BarCode\ApiException` for API failures. For many `4xx` failures, `getResponseObject()` contains a parsed `ApiErrorResponse`. |
| 106 | + |
| 107 | +## Reuse the common patterns |
| 108 | + |
| 109 | +Generate and save a QR code: |
| 110 | + |
| 111 | +```php |
| 112 | +use Aspose\BarCode\Model\BarcodeImageFormat; |
| 113 | +use Aspose\BarCode\Model\CodeLocation; |
| 114 | +use Aspose\BarCode\Model\EncodeBarcodeType; |
| 115 | +use Aspose\BarCode\Requests\GenerateRequestWrapper; |
| 116 | + |
| 117 | +$request = new GenerateRequestWrapper(EncodeBarcodeType::QR, 'hello from PHP'); |
| 118 | +$request->image_format = BarcodeImageFormat::Png; |
| 119 | +$request->text_location = CodeLocation::None; |
| 120 | + |
| 121 | +$file = $generateApi->generate($request); |
| 122 | +$file->rewind(); |
| 123 | +file_put_contents('qr.png', $file->fread($file->getSize())); |
| 124 | +``` |
| 125 | + |
| 126 | +Recognize a local file stream: |
| 127 | + |
| 128 | +```php |
| 129 | +use Aspose\BarCode\Model\DecodeBarcodeType; |
| 130 | +use Aspose\BarCode\Requests\RecognizeMultipartRequestWrapper; |
| 131 | + |
| 132 | +$file = new SplFileObject('qr.png', 'rb'); |
| 133 | +$result = $recognizeApi->recognizeMultipart( |
| 134 | + new RecognizeMultipartRequestWrapper(DecodeBarcodeType::QR, $file) |
| 135 | +); |
| 136 | + |
| 137 | +foreach ($result->getBarcodes() as $barcode) { |
| 138 | + echo $barcode->getBarcodeValue() . PHP_EOL; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Auto-scan bytes already in memory: |
| 143 | + |
| 144 | +```php |
| 145 | +use Aspose\BarCode\Model\ScanBase64Request; |
| 146 | +use Aspose\BarCode\Requests\ScanBase64RequestWrapper; |
| 147 | + |
| 148 | +$payload = base64_encode(file_get_contents('unknown.png')); |
| 149 | +$result = $scanApi->scanBase64( |
| 150 | + new ScanBase64RequestWrapper( |
| 151 | + new ScanBase64Request(['file_base64' => $payload]) |
| 152 | + ) |
| 153 | +); |
| 154 | +``` |
| 155 | + |
| 156 | +## Work inside this repo |
| 157 | + |
| 158 | +Read `references/repo-workflow.md` when the task changes SDK source, tests, snippets, package metadata, or generated code in `submodules/php`. |
| 159 | + |
| 160 | +Read `references/snippet-map.md` when the task needs example code, README-aligned snippets, or the closest existing pattern for a generate, recognize, or scan scenario. |
| 161 | + |
| 162 | +## Final checklist |
| 163 | + |
| 164 | +1. Use the correct package and namespace pair: Composer `aspose/barcode-cloud-php`, namespace `Aspose\BarCode\...`. |
| 165 | +2. Construct API classes as `new ...Api(null, $config)`. |
| 166 | +3. Pass the matching request-wrapper object into every SDK call. |
| 167 | +4. Use GET only for public URLs, multipart for local files, and base64-body requests for bytes already in memory. |
| 168 | +5. Base64-encode request payloads yourself before `recognizeBase64()` or `scanBase64()`. |
| 169 | +6. Treat generate responses as `SplFileObject` streams and recognize or scan responses as `BarcodeResponseList`. |
| 170 | +7. When changing the repo, validate with the submodule workflow in `references/repo-workflow.md`. |
0 commit comments