• Home
  • About
  • Services
    • Technical SEO
    • Local SEO Services
  • Tools
    • Redirects Generator
    • HREFLANG Tag Generator
    • XML Sitemap Generator
    • XML Sitemap URL Extractor
    • Robots.txt Generator
    • Random URL Generator
    • Meta Robots Tag Generator
    • Organization Schema Generator
    • Article Schema Generator
    • Product Schema Generator
    • Local Business Schema Generator
    • FAQPage Schema Generator
    • WordPress Metabox Generator
    • Plugins & Scripts
  • Tutorials Hub
    • Technical SEO
    • WordPress
    • PHP
    • JavaScript
    • Python
    • Google Tag Manager
    • Shopify
    • Other Guides & Tutorials
    • General Discussion
    • View All
  • Contact
    • Hub
      New posts Search forums
    • What's new
      New posts New profile posts Latest activity
    • Members
      Current visitors New profile posts Search profile posts
    Log in Register
    What's new Search

    Search

    By:
    Advanced search…
    • New posts
    • Search forums
    Menu
    Log in

    Register

    Install the app
    • Home
      • Hub
      • PHP Scripts & Snippets
      You are using an out of date browser. It may not display this or other websites correctly.
      You should upgrade or use an alternative browser.

      Simple CORS Proxy Script

      • Thread starter Chris
      • Start date Mar 19, 2023
      C

      Chris

      Administrator
      Staff member
      • Mar 19, 2023
      • #1
      I have recently been studying CORS (Cross-Origin Resource Sharing) to help with removing barriers for competitor research activities such as fetching data, etc. Below is a very simple CORS proxy script. The PHP script fetches the contents of the URL, bypassing the CORS restriction and echo's the page's contents. It can be easily modded to sync with other scripts,

      Code:
      <!DOCTYPE html>
      
      <head>
      
          <title> Simple CORS Proxy Script</title>
      
          <script>
              function fetchProxy() {
                  const url = document.getElementById("url").value;
                  const proxyUrl = "cors_proxy.php?url=" + encodeURIComponent(url);
      
                  fetch(proxyUrl)
                      .then(response => response.text())
                      .then(data => {
                          document.getElementById("result").innerText = data;
                      })
                      .catch(error => {
                          console.error("Error fetching:", error);
                      });
              }
          </script>
      </head>
      
      <body>
          <h1> Simple CORS Proxy Script</h1>
          <input type="text" id="url" placeholder="Enter URL to fetch">
          <button onclick="fetchProxy()">Fetch</button>
      
          <pre id="result"></pre>
      </body>
      
      </html>



      PHP:
      <?php
      header("Access-Control-Allow-Origin: *");
      header("Content-Type: text/plain");
      
      if (isset($_GET["url"])) {
          $url = urldecode($_GET["url"]);
      
          $options = [
              "http" => [
                  "header" => "Content-Type: text/plain",
                  "method" => "GET"
              ]
          ];
      
          $context = stream_context_create($options);
          $content = file_get_contents($url, false, $context);
      
          if ($content === false) {
              http_response_code(400);
              echo "Error fetching URL";
          } else {
              echo $content;
          }
      
      } else {
          http_response_code(400);
          echo "Missing 'url' parameter";
      }
      
      ?>
       
      Last edited: Mar 19, 2023
      You must log in or register to reply here.
      Share:
      Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link

      New Posts

      • C
        add a number to the next column of data, increasing the number as the datapoint changes, and count how many times each datapoint appears
        • Started by Chris
        • Jul 20, 2023
        • Replies: 0
        Other Plugins & Tutorials
      • C
        How to connect to GPT-4 API with Node.js using PowerShell
        • Started by Chris
        • Jul 14, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Google Search Results Scraper - Node JS & Puppeteer
        • Started by Chris
        • Jul 2, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Detect unused JavaScript and CSS in Bulk
        • Started by Chris
        • Jun 29, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • C
        Google Sheets Apps Script to Generate Hreflang tags
        • Started by Chris
        • Jun 12, 2023
        • Replies: 0
        Technical SEO
      • C
        Deploying Noindex Meta Robots Tag with JavaScript with Tag Manager - 2 Methods
        • Started by Chris
        • Jun 9, 2023
        • Replies: 0
        Google Tag Manager Tutorials & Guides
      • C
        Guide to Google Search Operators
        • Started by Chris
        • Jun 9, 2023
        • Replies: 0
        Other Plugins & Tutorials
      • C
        Bookmarklet to inspect and identify images that are not using lazy loading on a webpage
        • Started by Chris
        • Jun 2, 2023
        • Replies: 0
        JavaScript Scripts & Snippets
      • Hub
      • PHP Scripts & Snippets

    Technical SEO Consultant

    Chris Lever - Freelance SEO Consultant Availible for Hire. Specialist in Technical SEO Audits, Strategy and Implementation.

    Need Help?

    • BB Code
    • Cookie Policy
    • Terms of Use
    • Privacy Policy
    • HTML Sitemap

    Learn More

    • About
    • Availability
    • Popular Posts
    • Projects & Scripts
    • Contact

    Get in Touch

    Chris Lever, Manchester, United Kingdom
    hello@chrisleverseo.com
    +44 0161 883 7660

    © 2023 Chris Lever SEO. All rights reserved. Powered by Xenforo | PF

    • This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
      By continuing to use this site, you are consenting to our use of cookies.
      Accept Learn more…