PHP 从 Whatsapp 对话云 api 获取图像

问题描述 投票:0回答:1

要下载从 webhook 接收到的图像,首先,我通过调用媒体端点检索 url,然后执行curl 调用

https://graph.facebook.com/v14.0/xxxxxxxxxxxxxx

其中 xxxxxxxxxxxxxx 是媒体 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);

$response 是:

(
    [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
)

此后,我对获得的 URL 进行新的卷曲调用


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是“出了问题”

我本来期望的是一个二进制 blob。

我哪里做错了?

感谢任何愿意给我答案的人!请注意,此问题出现在 Whatsapp Cloud API 中,而不是业务 API 中。预先感谢

php api cloud whatsapp
1个回答
0
投票

我遇到了浏览器错误的问题,并将标头中的用户代理更改为邮递员,如下所示:用户代理:PostmanRuntime/7.34.0 它起作用了 仅供未来的读者(或者我,如果我忘记了)

© www.soinside.com 2019 - 2024. All rights reserved.