GET-PLACES-TIKTOK-FACEBOOK

Get TikTok and Facebook content of a specific place.

Refer at github for template (updated at 21 NOV 2023):

Output Screenshots

TikTok

Facebook

Implementation on Your Website

form.php

A form where user can input the platform of choice ("tiktok" or "facebook"), mainPlace (e.g. "Ipoh"), and name (e.g. "10 Studio Cafe")

get-places-tiktok-facebook.php

Import

Importing Google Fonts and CSS

HTML Layout

The main layout which consists of the banner (TikTok/Facebook), and the content section which displays the TikTok/Facebook content for a specific place.

JavaScript for building the layout

Responsible for building the layout after the content is retrieved.

PHP cording for streams to TikTok or Facebook content

  1. Retrieve Platform Value from POST Data:

    • Obtain the "platform" value from the POST data using $_POST["platform"].

  2. Display Banner Based on Platform:

    • Use an if-else statement to determine the platform.

    • If the platform is "tiktok," display a JavaScript banner using showBanner('tiktok').

    • If the platform is not "tiktok," display a JavaScript banner with the actual platform value.

  3. Flush Output Buffer:

    • Flush the output buffer to ensure immediate display of content in the browser.

  4. Retrieve Additional POST Data:

    • Get additional POST data such as "mainPlace" and "name" using $_POST["mainPlace"] and $_POST["name"].

  5. URL Encode Name and Main Place:

    • Encode the "name" and "mainPlace" variables using urlencode().

  6. Retrieve Platform Value Again:

    • Reassign the "platform" variable with the value from the POST data.

  7. Process Based on Platform:

    • If the platform is "tiktok":

      • Construct a search text for TikTok by concatenating encoded name, encoded main place, and "tiktok."

      • Create a Google search URL using the constructed search text.

      • Fetch content from the Google search page using file_get_contents().

      • Parse the HTML content using a DOMDocument and XPath.

      • Extract TikTok discover URLs from anchor tags and store them in an array.

      • If the array is empty, display a JavaScript function call for no TikTok results.

      • Otherwise, call the forTikTok function with the first URL from the array.

    • If the platform is "facebook":

      • Construct a search text for Facebook by concatenating encoded name, encoded main place, and "facebook."

      • Call the forFacebook function with the constructed search text.

PHP for TikTok content

  1. Fetch TikTok Page Content

    • Use file_get_contents($url) to retrieve the content of the TikTok page specified by the given URL.

  2. Initialize Arrays for Data Storage

    • Create arrays to store various data extracted from the TikTok page, including video views, usernames, likes, titles, tags, tag views, image URLs, and links.

  3. Initialize Error Flag

    • Set a flag, $somethingWong, to false initially. This flag will be used to check if something went wrong during the extraction process.

  4. Filter Author Links

    • Load the HTML content into a DOMDocument.

    • Use XPath to find specific <div> elements with the class tiktok-1as5cen-DivWrapper e1cg0wnj1.

    • Extract author anchors (<a> tags) and construct complete URLs by appending them to "https://www.tiktok.com".

    • Store the author links in the linksArray.

  5. Filter Video Views

    • Use XPath to find <strong> elements within a <div> with the class tiktok-k7smtc-DivCoverFooter e1aajktk7.

    • Extract the text content of the <strong> elements, representing the number of likes, and store them in the videoViewsArray.

  6. Filter Usernames

    • Use XPath to find <p> elements with the class tiktok-18g0m3y-PInfo-StyledH3UniqueId e1aajktk12.

    • Extract the text content of the <p> elements, representing usernames, and store them in the usernameArray.

  7. Filter Likes

    • Use XPath to find <span> elements with the class tiktok-1cgeagm-SpanLikes e1aajktk13.

    • Extract the text content of the <span> elements, representing the number of likes, and store them in the likesArray.

  8. Filter Title

    • Use XPath to find <div> elements with the class tiktok-156kx37-DivVideoDescription e1aajktk10.

    • Extract the text content from the 'title' attribute of the <div> elements and store them in the titleArray.

  9. Filter Tags and Views

    • Use XPath to find <div> elements with the class tiktok-1s2ovfk-DivContent eabhyw07 that contain the word 'views'.

    • Extract tag names, views, and construct tag links based on the content of these <div> elements.

    • Store tag names, views, and tag links in tagArray, tagViewsArray, and tagLinksArray, respectively.

    • Set $somethingWong to true if no matching <div> elements are found.

  10. Filter Images

    • Use XPath to find <div> elements with the class tiktok-1jxhpnd-DivContainer e1yey0rl0.

    • Extract image tags (<img> elements) and get the source link (URL) of each image.

    • Store the image URLs in the imageURLArray.

  11. Check for Errors

    • If $somethingWong is true (indicating an issue during tag and view extraction), echo a JavaScript script to call the noResult() function.

  12. Display Results

    • If extraction is successful, echo JavaScript scripts to convert PHP arrays to JavaScript variables.

    • Call the generateSectionHeaders() function to display the results using JavaScript.

PHP for Facebook content

  1. Initialize Google Search URL:

    • Construct a Google search URL for images related to the provided keyword on Facebook.

  2. Fetch Google Search Results:

    • Use file_get_contents to fetch the content of the Google search results page using the constructed URL.

  3. Create DOMDocument:

    • Create a new DOMDocument to parse the HTML content of the Google search results.

  4. Load HTML into DOMDocument:

    • Load the HTML content into the DOMDocument and set up XPath for querying.

  5. XPath Query for Href Attributes:

    • Use XPath to query for href attributes of <a> elements within tables with a specific class (GpQGbf).

  6. Process Facebook URLs:

    • Loop through the found <a> elements and extract relevant Facebook URLs.

  7. Check for Facebook URLs:

    • Check if the decoded URL contains 'facebook.com'.

  8. Filter Facebook URLs:

    • Differentiate between Facebook post URLs and Facebook photo URLs.

  9. Process Facebook Post URLs:

    • For post URLs, remove unwanted parts and check for characters after "/posts/".

  10. Process Facebook Photo URLs:

    • For photo URLs, apply specific conditions to filter out unwanted URLs.

  11. Check for Series of Numbers in Photo URLs:

    • Use a regular expression to check if there is a series of numbers before "/photos/" in the given URL.

  12. Display Results or No-Result Message:

    • Check if any processed URLs are found, and either display the processed URLs or show a no-result message.

  13. Remove Repeating Links:

    • Remove repeating links from the array of processed URLs to ensure uniqueness.

  14. Display Processed URLs:

    • Echo JavaScript code to set a variable with JSON-encoded data containing the processed URLs.

  15. Trigger JavaScript Function:

    • Echo JavaScript code to trigger the addLocationBlocks function, which likely handles the display of the processed URLs.

Last updated