提问人:izzymo 提问时间:11/6/2023 最后编辑:izzymo 更新时间:11/6/2023 访问量:22
如何从 Meta API 下载媒体 (Whatsapp Media)
How to download Media from the Meta API (Whatsapp Media)
问:
我已经按照官方链接上的文档进行了操作,并检索了媒体 URL https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media/#download-media
现在,一旦我有了URL,我就使用下面的PHP代码下载保存在FB服务器上的文件,该文件需要发送授权令牌,因此我在PHP中使用CURL文件。
但是,我保存了空文件。
$ch = curl_init($url);
$file_name = basename($url);
$file_loc = "./download/" . $MediaID . ".jpg" ;
$fp = fopen($file_loc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $MetaToken) );
$result = curl_exec($ch); curl_close($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
fwrite($fp, $body);
fclose($fp);
我也试过这样:
$file_loc = "./download/" . $MediaID . ".jpg" ;
$fp = fopen($file_loc, 'wb');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $waToken),
));
$response = curl_exec($curl); curl_close($curl);
fwrite($fp, $response); fclose($fp);
答:
0赞
izzymo
11/6/2023
#1
唷!它最终通过更改用户代理起作用
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => "YourAppName/2.0",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array('Authorization: Bearer ' . $waToken),
));
$result = curl_exec($curl); curl_close($curl);
list($headers, $content) = explode("\r\n\r\n", $result, 2);
foreach (explode("\r\n", $headers) as $hdr) {
if ( strpos($hdr, "Content-Disposition") !== false ) { $ext = substr(strrchr($hdr, '.'), 1); }
}
$file_loc = "./download/" . $MediaID . "." . $ext ;
$fp = fopen($file_loc, 'wb');
fwrite($fp, $content); fclose($fp);
评论