$apiKey = 'apiKey';
$url = "https://krishnaapps.com/api/udemy/courses/{$apiKey}";
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); if(curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
$courses = json_decode($response, true);
print_r($courses); // Display the course data
}
curl_close($ch);
Explanation:
- Initializes a cURL session.
- Sets the URL with the API key.
- Executes the cURL session and retrieves the response.
- Closes the cURL session.
Endpoint: Get Download Links
- URL:
https://krishnaapps.com/api/udemy/download/{id}/{api_key}
- Method: GET
- Description: Retrieves download links for course files,
including the file size and download link.
Prerequisites
- API Key: Users must have a valid API key to access the
endpoint.
PHP Integration Using cURL
Step 1: Initialize a cURL Session
$apiKey = 'apiKey';
$courseId = 'courseId';
$url = "https://krishnaapps.com/api/download/{$courseId}/{$apiKey}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
$files = json_decode($response, true);
foreach ($files as $file) {
echo "Size: " . $file['size'] . "\n";
echo "Download Link: " . $file['download_link'] . "\n";
}
}
curl_close($ch);
Explanation:
- Initialize cURL: Initialize a cURL session with the endpoint
URL.
- Execute cURL: Executes the cURL request and retrieves the
response.
- Process Response: Decodes the JSON response and outputs the
file details.
Java Integration Using Retrofit
Step 1: Add Retrofit Dependency
Add the following dependencies to your build.gradle file:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
Step 2: Create the API Interface
Create an interface to define the API endpoints:
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface KrishnaAppsApi {
@GET("udemy/courses/{api_key}")
Call<List<Course>> getCourses(@Path("api_key") String apiKey); }
Step 3: Define the Data Model
Create a class to model the course data:
public class Course {
private String title;
private String slug;
private String description;
private String objectives;
private String prerequisites;
private String targetAudiences;
private String shortDescription;
private String image;
private String categoryId;
private String duration;
private String lectures;
private String views;
private String tags;
private String authorName;
private String authorImage;
private String authorUrl;
private boolean featured;
private String createdAt;
private String updatedAt;
// Getters and Setters
}
Step 4: Initialize Retrofit and Make a Request
Initialize Retrofit and call the API to fetch the courses:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import java.util.List;
public class Main {
public static void main(String[] args) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://krishnaapps.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
KrishnaAppsApi api = retrofit.create(KrishnaAppsApi.class);
Call<List<Course>> call = api.getCourses("apiKey");
call.enqueue(new Callback<List<Course>>() {
@Override
public void onResponse(Call<List<Course>> call, Response<List<Course>> response) {
if (response.isSuccessful() && response.body() != null) {
List<Course> courses = response.body();
courses.forEach(course -> {
System.out.println(course.getTitle());
// Print other course details as needed
});
} else {
System.out.println("Request failed: " + response.code());
}
}
@Override
public void onFailure(Call<List<Course>> call, Throwable t) {
t.printStackTrace();
}
});
}
}
Explanation:
- Initializes Retrofit with the base URL and Gson converter.
- Creates an instance of the API interface.
- Makes an asynchronous request to fetch the courses.
- Processes and displays the course data upon successful response.