Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.5 kB
3
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Models\Proxy;
use App\Models\GTMCodes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class HandleUrlController extends Controller
{

    //private string $id;
    //private static array $gtm_array;
    public function proxy(Request $request)
    {
        $url = $request->query('url');
        try {
            // Make a request to the target URL using Laravel's HTTP client
    
            
            // Extract all GTM codes from the HTML response
            $response = Http::get($url);
            $html = $response->body();
            $gtmCodes = [];

            $pattern = '/GTM-([a-zA-Z0-9]+)/i';
            preg_match_all($pattern, $html, $matches);

            if (!empty($matches[1])) {
                $gtmCodes = $matches[1];
                //$unique = array_unique($gtmCodes);
            }
                                                // yawaa hahaha murag amaw laravel
            return response()->json(['gtmCodes' => array_unique($gtmCodes), 'response' => $html]);
        } catch (\Exception $e) {
            // Handle errors (e.g., invalid URL, connection issues)
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }



    public function storeProxy(Request $request)
    {
       //$gtmID = new self::$id;
        try {
            // dd($request->all());
            $url = $request->url;
            $status = $request->status;

          $proxyhit = Proxy::create([
                'url' => $url,
                'status' => $status
            ]);

            $id = $proxyhit->id;

            // Retrieve GTM codes from the request
            $gtmCodes = $request->gtmCodes; // Assuming gtmCodes is sent in the request

          // Store GTM codes in the database
          foreach ($gtmCodes as $gtmCode) {
              GTMCodes::create([
                  'id' => $id,
                  'gtm_codes' => $gtmCode
              ]);
          }


            return response()->json(['success' => 'proxy added'], 200);
        } catch (\Throwable $err) {
            return response()->json(['error' => $err->getMessage()], 500);
        }
    }

    public function checkDuplicateUrl(Request $request)
    {
        try {
            $url = $request->url;
            $url_data = Proxy::where('url', $url)->first();

            if ($url_data) {
                return response()->json(['success' => 'url exist', 'status_code' => 200], 200);
            } else {
                return response()->json(['error' => 'url did not exist ', 'status_code' => 404]);
            }
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function listAllUrl()
    {
        try {
            $list = Proxy::all();
            return view('index', compact('list'));
        } catch (\Throwable $th) {
            throw $th;
        }
    }


    public function fetchData()
    {
        try {
            $list = Proxy::all();
            return view('list.url-list', compact('list'));
        } catch (\Throwable $th) {
            throw $th;
        }
    }
    public function fetchGTMCodes()
    {
        try {
            $gtm_codes = GTMCodes::all();
            return view('create-a-component-for-gtm-codes', compact('gtm_codes'));
        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
Leave a Comment