Please tell me why i must put "\" before \SoapClient. When i delete "\", then i have a error.
public function indexAction($name)
{
try {
$client = new \SoapClient('some WSDL', array('trace' => 1));
$a = array('Login'=>'1', 'Password'=>'1', 'LetterNo'=>'1');
$response = $client->__soapCall('GetTracking', array($a));
ladybug_dump($response->GetTrackingResult->Status);
} catch (\SoapFault $e) {
var_dump($e->getMessage(), $client->__getLastResponse()); die();
}
return array('response' => $response);
}
Thanks for help
...because your controller is in a namespace, so your call to instantiate SoapClient without the root slash tries to load that object from the current namespace.
App\Controller\SoapClient instead of SoapClient.
You can use a use statement at the top of your controller to bring it into scope. use SoapClient; and you'll be good to go.
Related
I am new to Laravel and have an Issue regarding the Handler.php File.
I am trying to create a class that takes an exceptions and transforms it into a JSON Response.
Sadly though, upon calling the constructor a series of Errors are thrown:
(ErrorErrorErrorErrorErrorErrorErrorErrorErrorErrorErrorSymfony\Component\ErrorHandler\Error\FatalError)
My code:
render() in Handler.php:
public function render($request, Throwable $exception)
{
$errorResource = new ErrorResource($exception);
return $errorResource->getJsonResponse();
}
class ErrorResource in ErrorResource.php:
<?php
namespace Transformers;
use Throwable;
class ErrorResource
{
private $exception;
private $defaultCodes = [TypeError::class => 400];
private $defaultMessages = [TypeError::class => 'Untgültige URL Parameter'];
function __construct(Throwable $exception)
{
$this->exception = $exception;
}
public function getJsonResponse($exception)
{
$codeToThrow = 500;
$messageToThrow = "Internal Server Error";
$type = get_class($this->exception);
if (empty($exception->getCode())) {
$codeToThrow = $this->defaultCodes[$type];
} else {
$codeToThrow = $exception->getCode();
}
if (empty($exception->getMessage())) {
$messageToThrow = $this->defaultMessages[$type];
} else {
$messageToThrow = $exception->getMessage();
}
return response()->json(array(
'Type' => $type,
'Message' => $messageToThrow
), $codeToThrow);
}
}
I have also tried to move the method getJsonResponse() to the Handler.php file and call it from there, but without any luck.
I am really confused as to why I am not allowed to do certain things with the $exception variable (I have also tried to create a clone of this object - but the same error occures)
I hope you can help me resolving this issue,
Greetins,
Franz
The issue is, that PHP is call by value. That is why it is implicitely trying to clone an unclonable object -> Error. To resolve this issue one can use wrapper objects, but I decided to simply use call by reference (https://www.javatpoint.com/php-call-by-reference)
Hi am trying to run a function from wsdl. How to run this function?
When i am running get functions i am getting this
array(3) { [0]=> string(797) "ClientSoap createClient(string $client_name, string $client_password, string $first_name, string $last_name)" }
I want to run the createClient function .
I already tried using this code
$client = new SoapClient('mywsdl.wsdl');
$data = $client->createClient("samplecustomer", "samplepass", "first", "last");
print_r($data);
I am getting an error that createClient does not exist. Any ides?
Thanks in advance
I don't know if that can help you but you can add a try catch surround the declaration.
try {
$soapClient = new \SoapClient($url. '?wsdl', $options);
} catch (\SoapFault $e) {
print_r($e->getMessage();
}
So I am messing around with symfony router component and I created a small wrapper.
One thing that came up was how do I get a request to throw a 500 in unit tests? The method in question is:
public function processRoutes(Request $request) {
try {
$request->attributes->add($this->_matcher->match($request->getPathInfo()));
return call_user_func_array($request->attributes->get('callback'), array($request));
} catch (ResourceNotFoundException $e) {
return new RedirectResponse('/404', 302);
} catch (Exception $e) {
return new RedirectResponse('/500', 302);
}
}
And the test in question is:
public function testFiveHundred() {
$router = new Router();
$router->get('/foo/{bar}', 'foo', function($request){
return 'hello ' . $request->attributes->get('bar');
});
$response = $router->processRoutes(Request::create('/foo/bar', 'GET'));
$this->assertEquals(500, $response->getStatusCode());
}
Right now the test will fail because we are defined and the status code will be 200. Is there something special I can do to the Request object I create, to make it throw a 500?
I think you got several options here you can play with:
Decide that a specific path will always throw an exception.
This will force you to make some changes in your code.
public function processRoutes(Request $request) {
...
if ($request->getRequestUri() == '/path/that/throws/exception') {
throw Exception('Forced to throw exception by URL');
}
...
}
public function testFiveHundred() {
...
$response = $router->processRoutes(Request::create('/path/that/throws/exception', 'GET'));
...
}
Make a DummyRequest object that will extends your original Request class and make sure this object will raise an Exception (for example - you know for sure that you use the getPathInfo(), so you can use this).
class DummyRequest extends Request {
public function getPathInfo() {
throw new Exception('This dummy request object should only throw an exception so we can test our routes for problems');
}
}
public function testFiveHundred() {
...
$dummyRequest = new DummyRequest();
$response = $router->processRoutes($dummyRequest);
...
}
Since the function getRequestUri of our $dummyRequest throws an exception, your call to $router->processRoutes will have our dummy to throw that exception.
This is a general idea, you would probably need to play a bit with the namespaces and the functions there (I didn't test it, however this should work).
I searched and there are a lot of answers but I cannot find one I need because I do not know even how to create correct question. here is the example.
$app->map('/v1/:module/:group/:action(/:id)', function ($module, $group, $action, $id = NULL) use ($app) {
$method = ucfirst($app->request->getMethod());
$file = "modules/{$module}/{$group}/{$method}{$action}.php";
if(!file_exists($file)) {
$app->halt(404, Error::_('API Processor was not found!', 404));
}
include_once $file;
$app->stop();
})
This is my API method by slim restful framework. Now for this Error::_('API Processor was not found!', 404) I have
class Error {
public static function _($msg, $code = 500) {
global $module, $group, $action;
return json_encode(array(
'error' => true,
'code' => $code,
'message' => $msg,
'module' => $module
));
}
}
What I want os to get access to $module, $group, $action variables without passing them into that function. But in my case $module is NULL.
{
"error":true,
"code":404,
"message":"API Processor was not found!",
"module":null
}
Possible?
You should be able to meet those requirements, if I understood your question correctly, by using the Slim Error Handling functionality. If it were my project, I'd create a custom exception to throw wherever you're planning on using your custom error function.
NOTE: All of the code below is untested and written off the top of my head. Caveat emptor and all that.
class CustomErrorException extends \Exception
{
}
Then I would throw that exception wherever I'd otherwise use my custom error function.
if(!file_exists($file)) {
throw new CustomErrorException('API Processor was not found!', 404);
}
Finally, I'd write an error function that looks something like this:
$app->error(function (\Exception $e) use ($app) {
if ($e instanceof CustomErrorException) {
// Parse $path to get $module, $group, and $action
// (Seems like that would work based on the route in your example: '/v1/:module/:group/:action(/:id)')
$path = $app->request->getPath();
// Possible new method signature for Error::_
Error::_($e->getMessage(), $e->getCode(), $module, $group, $action);
// Render an error page, $app->halt(), whatever.
}
});
That should help DRY your code up a bit and allow you to dump those global variables.
I am trying to set up a restful api using yii. Trying to add a wrapper that takes the results from the code being ran in the controller and returning it in json format. I am also trying to have it catch any errors[try-catch] and returning them in json format.
Right now all I can think of doing is something similar to the code below...I'd like the ability to not have to add a try/catch everytime.
class UserController extends Controller{
public function actionIndex($user_id = null){
$response = new API_Response();
try{
$response->success = true;
$response->data = array("data"=>"data goes here...");
}catch(Exception $e){
$response->success = false;
$response->message = $e->getMessage();
}
$response->send();
}
With more research, found I could override the api handler per each controller so now I don't have to write a whole bunch of try-catches.
function init(){
$this->api_resp = new API_Response();
Yii::app()->attachEventHandler('onException',array($this, 'handleApiError'));
}
public function handleApiError(CEvent $e){
if($e instanceof CExceptionEvent){
$this->api_resp->error = $e->exception->getMessage();
$this->api_resp->send();
}else{
$this->api_resp->error = Yii::t('app', 'error.unknown');
$this->api_resp->send();
}
}