ตัวอย่างที่นำเสนอ จะเป็นการยืนยันตัวตนด้วย api key แบบ bacic
โค้ด: เลือกทั้งหมด
$endpoint = 'URL Web Service';
$apiKey = 'API KEY';
$filepath = 'Full Path File ';
$httpInterface = HttpFactory::getHttp(); // สร้าง Object ไว้จัดการกับ http จาก lib ของ Joomla
//html basic authorization
$httpInterface->setOption('headers.Authorization', 'Basic ' . base64_encode('api:' . $apiKey));
$response = $httpInterface->post($endpoint, file_get_contents($filepath));
// รับ $response เพื่อทำงานต่อ
// ตัวอย่าง
if (200 == $response->code || 201 == $response->code)
{
$json = json_decode($response->body);
if (isset ($json->error))
{
Factory::getApplication()->enqueueMessage(Text::_('ข้อความแจ้ง ERROR ถ้ามีข้อผิดพลาด'), 'error');
return null;
}
else
{
//reset iface
$httpInterface = HttpFactory::getHttp();
$image = $httpInterface->get($json->output->url)->body;
file_put_contents($filepath, $image); // Save ทับไฟล์เดิม
return $json->output->size;
}
}