Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jessebyarugaba/Unofficial-Uganda-Securities-Exhange-API/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The Uganda Securities Exchange API library retrieves market data through web scraping techniques. Since the USE does not provide an official public API, this library scrapes data directly from their website using the Simple HTML DOM Parser.This library uses the Simple HTML DOM Parser to parse and extract data from HTML pages.
How it works
The library scrapes data from multiple sources to provide comprehensive market information:Market snapshot data
ThegetAllPortfolioCompanies() method scrapes the market snapshot page to retrieve all listed companies:
/home/daytona/workspace/source/PortfolioManager.php:18-20
The method:
- Loads the HTML from the market snapshot page
- Finds the table containing company data using CSS selectors
- Iterates through table rows (
<tr>) and columns (<td>) - Extracts text content and constructs company URLs
- Returns data in JSON format
Company details
ThegetCompanyDetails() method scrapes individual company pages to extract detailed information:
/home/daytona/workspace/source/PortfolioManager.php:78-99
This method uses specific CSS selectors to extract:
- Company logo URL
- ISIN (International Securities Identification Number)
- Listing date
- Shares issued
- Market capitalization
- Contact information (address, phone, email, website)
Stock price data
ThegetPortfolioCompanyData() method retrieves historical stock price data from a JSON endpoint:
/home/daytona/workspace/source/PortfolioManager.php:125-128
This endpoint returns JSONP data, which the library cleans by removing the callback wrapper before parsing.
Exchange rate data
ThegetExchangeRateDetails() method scrapes currency data from African Financials:
/home/daytona/workspace/source/PortfolioManager.php:192-208
This method:
- Sets a custom user agent to avoid blocking
- Creates a stream context with appropriate headers
- Extracts exchange rate values, changes, and ranges
- Includes disclaimer content from the source
- Tracks execution time for performance monitoring
Technical implementation
CSS selectors
The library uses CSS selectors to target specific HTML elements:Memory management
The library properly releases memory after parsing HTML:/home/daytona/workspace/source/PortfolioManager.php:66-67
Error handling
The library includes basic error handling for common scenarios:/home/daytona/workspace/source/PortfolioManager.php:23-25
Data format
All methods return data in JSON format with appropriate headers:Limitations
Website structure dependency
The library depends on the current HTML structure of the USE website. If the website redesigns its pages or changes CSS classes, the scraping logic will need to be updated.Performance considerations
Web scraping involves:- HTTP requests to external websites
- HTML parsing overhead
- Potential network latency
The exchange rate method includes execution time tracking to help monitor performance:
Rate limiting
Making too many requests in a short period may result in:- IP blocking
- Temporary access restrictions
- Server-side rate limiting
Best practices
- Cache results: Store scraped data to reduce unnecessary requests
- Handle errors gracefully: Always check if HTML loaded successfully before parsing
- Respect robots.txt: Check the website’s robots.txt file for scraping policies
- Monitor for changes: Regularly test your scraping logic to detect website changes
- Use appropriate user agents: Some websites block requests without proper user agent headers
Example usage
/home/daytona/workspace/source/PortfolioManager.php:261-278