Repository navigation
Commit 3b9cec0
authored
Add Image API support with uploadImage() (#11)
* Add Image API support with uploadImage()
Uploads a JPG, PNG, or WebP image to POST /image as multipart/form-data
and returns the decoded response, whose image_id can be passed to
Google Lens and other engines that accept uploaded images. Mirrors
upload_image in serpapi-ruby and serpapi-python:
- accepts a file path or a stream; seekable streams are rewound first,
as Ruby does, and uploaded from a temporary copy because cURL uploads
by path and CURLStringFile needs PHP 8.1
- sends the same parameters as any other request (api_key, source,
default search parameters) as form fields, so the key stays out of
the URL; per-call api_key and timeout work as for search()
- sends only the file's base name; without one, cURL would send the
full local path
- errors raise SerpApiException with the same context as searches
The persistent cURL handle now resets the method on every GET.
Without CURLOPT_HTTPGET, a search after an upload went out as a
multipart POST. Uploads also drop the Expect: 100-continue header:
libcurl before 7.69 sends it with bodies over 1 KB, SerpApi does not
answer it, and each upload stalled for a second.
The live integration test uploads a 27 KB public domain thumbnail of
the Mona Lisa from Wikimedia Commons and searches it with Google Lens.
* Reject empty image streams and fix the form separator
stream_copy_to_stream returns 0, not false, for an empty or already
consumed stream, such as a pipe that was read before. The upload then
sent an empty file and failed with the API's error. It now raises
SerpApiException before any request is sent.
formFields() passes '&' to http_build_query explicitly, so a custom
arg_separator.output in php.ini no longer breaks the split.1 parent 07c1dcb commit 3b9cec0
7 files changed
Lines changed: 391 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
328 | 329 | | |
329 | 330 | | |
330 | 331 | | |
| 332 | + | |
| 333 | + | |
331 | 334 | | |
332 | 335 | | |
333 | 336 | | |
| |||
907 | 910 | | |
908 | 911 | | |
909 | 912 | | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
910 | 934 | | |
911 | 935 | | |
912 | 936 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
| 254 | + | |
| 255 | + | |
253 | 256 | | |
254 | 257 | | |
255 | 258 | | |
| |||
478 | 481 | | |
479 | 482 | | |
480 | 483 | | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
481 | 505 | | |
482 | 506 | | |
483 | 507 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
319 | 319 | | |
320 | 320 | | |
321 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
322 | 389 | | |
323 | 390 | | |
324 | 391 | | |
| |||
347 | 414 | | |
348 | 415 | | |
349 | 416 | | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
350 | 442 | | |
351 | 443 | | |
352 | 444 | | |
| |||
367 | 459 | | |
368 | 460 | | |
369 | 461 | | |
370 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
371 | 468 | | |
372 | | - | |
373 | 469 | | |
374 | 470 | | |
375 | 471 | | |
| |||
457 | 553 | | |
458 | 554 | | |
459 | 555 | | |
460 | | - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
461 | 582 | | |
462 | 583 | | |
463 | 584 | | |
464 | 585 | | |
| 586 | + | |
465 | 587 | | |
466 | 588 | | |
467 | 589 | | |
468 | | - | |
| 590 | + | |
469 | 591 | | |
470 | 592 | | |
471 | 593 | | |
472 | 594 | | |
473 | | - | |
| 595 | + | |
474 | 596 | | |
475 | 597 | | |
476 | 598 | | |
477 | 599 | | |
478 | 600 | | |
479 | | - | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
480 | 615 | | |
481 | 616 | | |
482 | 617 | | |
| |||
588 | 723 | | |
589 | 724 | | |
590 | 725 | | |
591 | | - | |
| 726 | + | |
592 | 727 | | |
593 | 728 | | |
594 | 729 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
70 | 85 | | |
0 commit comments