curl request in udemy api using php and export data in excel file
To make any calls to Udemy REST API, you will need to create an API client. API client consists of a bearer token, which is connected to a user account on Udemy. To send authenticated requests, provide the client_id and client_secret values as a base64 encoded HTTP Authorization header. You may also like PHPExcel export mysql to Excel in Laravel 5 and PHPExcel export mysql to excel in Codeigniter.
PHP Code
<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
$default_border = array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('rgb' => '000000'),
);
$acc_default_border = array(
'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array('rgb' => 'c7c7c7'),
);
$outlet_style_header = array(
'font' => array(
'color' => array('rgb' => '000000'),
'size' => 10,
'name' => 'Arial',
'bold' => true,
),
);
$top_header_style = array(
'borders' => array(
'bottom' => $default_border,
'left' => $default_border,
'top' => $default_border,
'right' => $default_border,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'ffff03'),
),
'font' => array(
'color' => array('rgb' => '000000'),
'size' => 15,
'name' => 'Arial',
'bold' => true,
),
'alignment' => array(
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
$style_header = array(
'borders' => array(
'bottom' => $default_border,
'left' => $default_border,
'top' => $default_border,
'right' => $default_border,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'ffff03'),
),
'font' => array(
'color' => array('rgb' => '000000'),
'size' => 12,
'name' => 'Arial',
'bold' => true,
),
'alignment' => array(
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
),
);
$account_value_style_header = array(
'borders' => array(
'bottom' => $default_border,
'left' => $default_border,
'top' => $default_border,
'right' => $default_border,
),
'font' => array(
'color' => array('rgb' => '000000'),
'size' => 12,
'name' => 'Arial',
),
'alignment' => array(
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
),
);
$text_align_style = array(
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
),
'borders' => array(
'bottom' => $default_border,
'left' => $default_border,
'top' => $default_border,
'right' => $default_border,
),
'fill' => array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'ffff03'),
),
'font' => array(
'color' => array('rgb' => '000000'),
'size' => 12,
'name' => 'Arial',
'bold' => true,
),
);
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:B1');
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Udemy API Report');
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($top_header_style);
$objPHPExcel->getActiveSheet()->getStyle('B1')->applyFromArray($top_header_style);
$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Title');
$objPHPExcel->getActiveSheet()->setCellValue('B2', 'Url');
$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($style_header);
$objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray($style_header);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(75);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(75);
$row = 3;
$limit = 100;
$total_pages = ceil(1000 / $limit);
$array = array();
for($i=0; $i<=$total_pages; $i++)
{
header('Content-Type: application/json');
$url = "https://www.udemy.com/api-2.0/courses/?fields[user]&page=".$i."&page_size=".$limit."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Udemy-Client-Id:','X-Udemy-Client-Secret:',"Authorization:","Accept: application/json, text/plain, */*"));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result=curl_exec($ch);
curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_getinfo($ch,CURLINFO_HTTP_CODE);
$result = json_decode($result);
if(!empty($result->results))
{
foreach ($result->results as $value)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $value->title);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $value->url);
$row++;
}
}
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="udemy.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
Leave a Reply