0

Basically i want to input multiple site url in textarea and check header response of these input. my code mention below:

    use Illuminate\Http\Request;
    use Goutte\Client;
    //use GuzzleHttp\Client;
    use GuzzleHttp\Promise;
    
    class Controller extends BaseController
    {
        use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    
    
        public function Datanewsub(Request $request){
    
            $client = new \GuzzleHttp\Client();
            $url= $request->input('url');
            $arr = explode("\n", $url);
            foreach($arr as $x => $val){
                echo $val.'<br>';
                $res = $client->request('GET', $val);
                echo $res->getStatusCode().'<br>';
            }
    
            return view('scraper');
    
        }
    }

7
  • Do you want to show multiple urls in your <textarea>? Commented May 3, 2021 at 21:13
  • user put multi url in text area and i want to check header response of each url weather its 200 or 404 etc Commented May 3, 2021 at 21:15
  • Then why echo it if you just want to check if the status code is 200 or 404 Commented May 3, 2021 at 21:22
  • simply create an array and use if condition with if($res->getStatusCode() == 200 || $res->getStatusCode() == 404) then save the values in the associative array. Commented May 3, 2021 at 21:23
  • because its our requirement to print status of each url Commented May 3, 2021 at 21:24

1 Answer 1

2

You could handle it like following:


<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use Illuminate\Http\Request;

class Controller extends BaseController
{

    public function yourAction(Request $request){

        $client = new Client();
        $urls = explode("\n", $request->input('url'));

        $responses = [];
        foreach($urls as $key => $url){
            $response = $client->request('GET', $url);
            $responses[$url] = $response->getStatusCode();
        }

        return view('scraper', compact('responses'));
    }

}

And in your scraper.blade.php view file:

@foreach($responses as $key => $response)

    {{$key}} => {{$response}}

@endforeach

And you should definitely also add some exception handling within your foreach in the controller action - if there is invalid input for example you will probably get an exception from the guzzle http client.

Sign up to request clarification or add additional context in comments.

4 Comments

GuzzleHttp response 404 with all url except last one 200
How does your input exactly look like?
i input multi url in textarea and pass these url to foreach loop foreach($urls as $key => $url){ $response = $client->request('GET', $url); $responses[$url] = $response->getStatusCode(); }
Did you try to debug - it could be possible that you have a \r in all your URLS except the last one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.