Get Image using API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joostk
    Junior Member
    • Nov 2020
    • 21

    Get Image using API

    Dear,

    Im trying to get the image of an record via the API. All data comes in except the image:

    I have tried: https://docs.espocrm.com/development/api/attachment/ without success.

    This is the script:

    Code:
    // Fetch product information
    $response = $client->request('GET', 'Product');
    
    if (!$response || !isset($response['list'][0])) {
    die("Failed to retrieve product information.");
    }
    
    $product = $response['list'][0];
    
    $imageUrl = $url . "/api/v1/Attachment/file/" . $product['id'];
  • Jakub Grufik
    Senior Member
    • Aug 2022
    • 361

    #2
    Hello, what exactly do you mean by image of an record? What is the records entity type? Also in the code where you are trying to get an attechment via API you need to use ID of the attachment not the ID of the product itself.

    Comment

    • joostk
      Junior Member
      • Nov 2020
      • 21

      #3
      Thanks for your reply.

      The entity i want to get an image from is the Product entity from the salespack extension. I have added an field in the entity to upload an image with the product.

      Again thanks!

      Comment


      • Jakub Grufik
        Jakub Grufik commented
        Editing a comment
        Aight, then go to random product open its detail and then change the URL to this one https://your_crm_url.eu/api/v1/Produ...of_the_product and over there you will find the id of the image you are looking for and then you can use this ID in the API request to get the attachment

      • Jakub Grufik
        Jakub Grufik commented
        Editing a comment
        The request in your code will basically look similar to this: $imageUrl = $url . "/api/v1/Attachment/file/" . $product['imageId']; where "imageId" is the name of the field you have added to upload the photo of the product make sure you will use imageId not just image
        Last edited by Jakub Grufik; 09-13-2023, 01:22 PM.
    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #4
      Originally posted by joostk
      Dear,

      Im trying to get the image of an record via the API. All data comes in except the image:

      I have tried: https://docs.espocrm.com/development/api/attachment/ without success.

      This is the script:

      Code:
      // Fetch product information
      $response = $client->request('GET', 'Product');
      
      if (!$response || !isset($response['list'][0])) {
      die("Failed to retrieve product information.");
      }
      
      $product = $response['list'][0];
      
      $imageUrl = $url . "/api/v1/Attachment/file/" . $product['id'];
      You should provide the id of the image to the endpoint like below:

      PHP Code:
      // Fetch product information
      $response = $client->request('GET', 'Product');
      
      if (!$response || !isset($response['list'][0])) {
      die("Failed to retrieve product information.");
      }
      
      $product = $response['list'][0];
      
      $imageUrl = $url . "/api/v1/Attachment/file/" . $product['image_id']; // replace image with name of the field where you store the product image. ​ 
      
      Also it is much easier to espocrm/php-espo-api-client in your project.
      Rabii
      Web Dev

      Comment

      • joostk
        Junior Member
        • Nov 2020
        • 21

        #5
        Works great! Thanks

        Comment

        Working...