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
Related
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');
}
}
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();
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();
My problem:
I have a controller that calls a method from another controller for some information. However, Laravel isn't able to locate the class in that controller.
FatalErrorException in TradesController.php line 35: Class 'Profile'
not found
What I have tried:
I am using Laravel 5.2 and have created the controller with php artisan make:controller Profile to ensure that any possible internal pointers (in lack of better vocabulary) are created - even though my understanding is that Laravel 5.2 does this automatically as long as controllers are in the \app directory.
They both reside within \app\Http\Controllers
My code
TradesController calls class Profile
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Profile;
use Session;
use Auth;
class TradesController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function showInventory (Request $request){
.....
// Following three calls all fail
$profile = new Profile;
$profile = Profile()->linkToProfile();
$profile = Profile::linkToProfile();
.....
return($output);
}
}
MY class Profile-controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use Auth;
class ProfileController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function linkToProfile (Request $request) {
return("test");
}
}
php artisan make:controller xxx creates a class xxxController therefore your class is ProfileController not Profile.
// 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);