HTTP request in Laravel don't work - php

This is my route file:
Route::get('/','guzzle#guzzle');
And this is my controller class:
use App\Http\Requests;
use GuzzleHttp\Client;
class guzzle extends Controller
{
public function guzzle(){
$client = new GuzzleHttp\Client();
$request = $client->head('http://www.amazon.com');
$response = $request->send();
echo $response->getContentLength();
It gave me the following error message in my browser:
FatalErrorException in guzzle.php line 17:
Class 'App\Http\Controllers\GuzzleHttp\Client' not found
I don't know how to fix this issue. Who can help me?

Once you have imported the class:
use GuzzleHttp\Client;
You should not type the full class namespace while instantiating it:
wrong: new GuzzleHttp\Client();
right: new Client();

You can also get rid of the namespace prefixing with this:
$client = new \GuzzleHttp\Client();

Related

SoapClient and Laravel

I'm trying to send request using SoapClient to WSDL file but am receiving this error
Class 'App\Http\Controllers\SoapClient' not found
I tried to add \SoapClient->callSoap() or use SoapClient; am still getting error and my soap extension is enabled.
Here is my code in controller:
$client = new SoapClient('https://adm.com/api/service.php?wsdl');
$headerbody = array('OrgId'=>'4AWisEr','OrgKey'=>'ikaroXajiD');
$header = new SoapHeader('https://adm.com/api/service.php?wsdl','credentials',$headerbody);
$client->__setSoapHeaders($header);
$result = $client->emailExists($email);
return $result;
Just tried this and I did not get a namespace error:
<?php
namespace App\Http\Controllers;
use SoapClient;
class TestController extends Controller
{
public function index()
{
$client = new SoapClient('https://adm.com/api/service.php?wsdl');
}
}

include class with autoload.php but getting class not found error

I tried to use jasonmapper just as written in manual.
I required autoload.php file, and when construct JasonMapper object, I go class not found exception.
(1/1) FatalThrowableError
Class 'App\Http\Controllers\JsonMapper' not found
Here is my code
namespace App\Http\Controllers;
require __dir__.'/../../../vendor/autoload.php';
use Illuminate\Http\Request;
use App\Http\Games\Numbers;
class ApiController extends Controller
{
public function home()
{
$client = new \GuzzleHttp\Client();
$res = $client->request(
'GET',
$testurl
);
$json = json_decode($res->getBody());
$mapper = new JsonMapper();// error occurs at this line
$numbers = $mapper->map($json, new Numbers());
return json_encode($numbers);
}
}
If you don't "use" JsonMapper at the top of your script, PHP assumes that JsonMapper is in the App\Http\Controllers namespace, which it's not. That means in your script you must:
$mapper = new \JsonMapper();

Class 'App\Http\Controllers\Illuminate\Container\Container' not found

I want to parse an XML.
I writed this ResultController
<?php
namespace App\Http\Controllers;
use Auth;
use \App\User;
use Illuminate\Http\Request;
use XmlParser;
use Illuminate\Container\Container;
use Orchestra\Parser\Xml\Document;
use Orchestra\Parser\Xml\Reader;
class ResultController extends Controller
{
public function getResults()
{
$xml = XmlParser::load('http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllTeams?ApiKey=ZXRIQOWMCFARAWRQIMSLRXCTSZDOBNLOTYWXYXMZYGDSENFSRB');
$app = new Illuminate\Container\Container;
$document = new Orchestra\Parser\Xml\Document($app);
$reader = new Orchestra\Parser\Xml\Reader($document);
$xml = $reader->load('http://www.xmlsoccer.com/FootballDataDemo.asmx/GetAllTeams?ApiKey=ZXRIQOWMCFARAWRQIMSLRXCTSZDOBNLOTYWXYXMZYGDSENFSRB');
$user = $xml->parse([
'users' => ['uses' => 'Team[Team_Id,Name]'],
]);
// dd($xml);
return view ('results.live');
}
}
I used use Illuminate\Container\Container; at top of the controller but it gaves me this error:
FatalErrorException in ResultController.php line 13: Class
'Illuminate\Container\Container\Controller' not found.
I cant understand what is wrong with it?
If you've used use keyword above -
use Illuminate\Container\Container;
use Orchestra\Parser\Xml\Document as OrchestraDocument;
use Orchestra\Parser\Xml\Reader as OrchestraReader;
you should use it inside method as (updated):
$app = new Container;
$document = new OrchestraDocument($app);
$reader = new OrchestraReader($document);
As you've used new Illuminate\Container\Container the php would find
your container as -
App\Http\Controllers\Illuminate\Container\Container, which isn't the
correct path, the use keyword helps php to recognize the namespace
of class Container
For more info see PHP Namespacing Docs
Hope this helps!

Fatal Error trying to use GuzzleHttp in Laravel 5.2

I'm trying to create a POST request using Laravel 5.2 and GuzzleHttp Client. I've successfully installed GuzzleHttp with Laravel but it just keeps repeating an error.
Fatal error: Call to undefined function App\Http\Controllers\API\Client()
Here is my code.
<?php
namespace App\Http\Controllers\API;
use Closure;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\API\APIConfiguration;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
class APIController extends Controller {
public function __construct(Request $request){
$this->request = $request;
}
public function doShardDetails(Request $request) {
$APIConfig = new APIConfiguration();
$client = Client();
$json = $APIConfig->jsonTemplate("Method");
$request = $client->post("IP:PORT", $json);
return $request;
}
}
I've been trying to fix this for hours, nothing on the internet. :(
You have a typo:
$client = Client();
You should create a new object:
$client = new Client();
Fatal error: Call to undefined function App\Http\Controllers\API\Client()
you need import Client class right way - with it own namespace, because namespace App\Http\Controllers\API do not have class name Client

Use crawler in controller

// src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php
namespace Acme\DemoBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DemoControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
$crawler = $client->request('GET', '/demo/hello/Fabien');
$this->assertGreaterThan(0, $crawler->filter('html:contains("Hello Fabien")')->count());
}
}
this working OK in my tests, but i would like use this crawler also in controller. How can i do it?
i make route, and add to controller:
<?php
// src/Ens/JobeetBundle/Controller/CategoryController
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class CategoryController extends Controller
{
public function testAction()
{
$client = WebTestCase::createClient();
$crawler = $client->request('GET', '/category/index');
}
}
but this return me error:
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /acme/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php on line 24
The WebTestCase class is a special class that is designed to be run within a test framework (PHPUnit) and you cannot use it in your controller.
But you can create a HTTPKernel client like this:
use Symfony\Component\HttpKernel\Client;
...
public function testAction()
{
$client = new Client($this->get('kernel'));
$crawler = $client->request('GET', '/category/index');
}
Note that you will only be able to use this client to browse your own symfony application. If you want to browse an external server you will need to use another client like goutte.
The crawler created here is the same crawler returned by WebTestCase so you can follow the same examples detailed in the symfony testing documentation
If you need more information, here is the documentation for the crawler component and here is the class reference
You shouldn't use WebTestCase in prod environment, because WebTestCase::createClient() creates test client.
In your controller you should do something like this (I recommend you to use Buzz\Browser):
use Symfony\Component\DomCrawler\Crawler;
use Buzz\Browser;
...
$browser = new Browser();
$crawler = new Crawler();
$response = $browser->get('/category/index');
$content = $response->getContent();
$crawler->addContent($content);

Categories