You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP调用WhatsApp Cloud API获取图片失败,请求排查问题

WhatsApp Cloud API 图片下载失败问题

我在通过Webhook接收图片后,按以下步骤尝试下载:

  1. 调用Media端点获取图片URL
    使用PHP代码发起CURL请求到https://graph.facebook.com/v14.0/[媒体ID],代码如下:

    $token = 'sdfsfsdfd';
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://graph.facebook.com/v14.0/xxxxx',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer '.$token,
        'Content-Type: application/json'
      ),
    ));
     
    $response = curl_exec($curl);
    curl_close($curl);
    echo $response."<hr>";
    
    $dati = json_decode($response);
    

    成功拿到响应,包含有效URL:

    (
        [url] => https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=790316572396xxx&ext=1659596318&hash=ATuHn61BbJOBYzugyRcP6O6UnyY2NSVh3Bb8v12OS3OCzQ
        [mime_type] => image/jpeg
        [sha256] => 1cf4a54f0d86c6603d10ad2e9836bc5a98edfabab4b5b8120822be59cbdcxxx0
        [file_size] => 253685
        [id] => xxxxx
        [messaging_product] => whatsapp
    )
    
  2. 请求图片URL下载
    使用拿到的$dati->url发起CURL请求,代码如下:

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => $dati->url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer '.$token
        
      ),
    ));
    
    $response = curl_exec($curl);
    
    if(curl_errno($curl)){
        throw new Exception(curl_error($curl));
    }
    
    $getstatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    
    if($getstatusCode == 200){
        
        echo 'Downloaded!<hr>';
            
        echo $response;
        
    } else{
        
        echo "Status Code: " . $getstatusCode;
        
    }
    

    但返回的$response是"something went wrong",不是预期的图片二进制数据。


问题原因及修正方案

1. 核心错误:多余的Authorization头

请求lookaside.fbsbx.com的图片URL时,不需要携带Authorization: Bearer头。该URL本身已包含hash、ext等验证参数,额外添加授权头会触发Facebook的验证逻辑,返回错误提示。

2. 可选优化:二进制数据处理

直接echo $response会将图片二进制输出为乱码,建议保存为本地文件或输出正确的图片响应头。

修正后的代码

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $dati->url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  // 移除多余的Authorization头
  CURLOPT_HTTPHEADER => array(),
));

$response = curl_exec($curl);

if(curl_errno($curl)){
    throw new Exception(curl_error($curl));
}

$getstatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl); // 记得释放CURL资源

if($getstatusCode == 200){
    // 方案1:保存为本地文件
    file_put_contents('downloaded_image.jpg', $response);
    echo '图片已成功保存到本地';
    
    // 方案2:直接输出到浏览器(需去掉前面的echo语句)
    // header('Content-Type: image/jpeg');
    // echo $response;
} else{
    echo "状态码: " . $getstatusCode;
}

额外注意事项

  • 图片URL的有效期有限(通常5分钟左右),需在获取后尽快发起下载请求。
  • 确保$dati->url是完整未转义的,json_decode后可直接使用,无需额外处理。

内容的提问来源于stack exchange,提问作者Senthil Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:42:24