I have to run an API on postmen, the project is based upon Laravel. The endpoint is mention below
`Route::get('order/detail/{order}', 'OrdersController#show');`
here is my order controller function:
public function show(Order $order)
{
dd("ok");
}
I am trying to run API using postmen and receiving 404 error
but when I removed {order} from endpoints, it worked. so my question is how can I run this API on postmen. Any help would be highly appreciable.
With Route::get('order/detail/{order}', 'OrdersController#show');
You must use the endpoint is http://localhost:8082/v3/order/detail/45487.
Not ...order/detail?order=45487
then
public function show($order) //$order is 45487
{
dd("ok");
}
Related
I'm using the Laravel Swoole Coroutine go function to do a HTTP request in order to achieve better performance. When I get the response from the external service, a new entry is created in the database with the data from the external service.
I want to be able to return a response from the controller with that new DB record. However, it appears that anything outside the go does not have access to anything that got assigned in the go function. I understand that that happens in a separate thread, but is there a way to implement this so that I can have access to the results inside the go function?
Please note that I have coroutine enabled globally and I only to use function as shown below:
public function store(User $user, Request $request) {
go(function () {
// get data from external API using Laravel HTTP Client
...
$user = User:create($data);
return response($user, 201)->send();
});
}
I have tried using the WaitGroup(), but it complains that the event loop has already been started if I wrap it with the Co\run function.
I have been trying to set up WhatsApp cloud API on my Laravel MVC project. I'm stuck trying to set up webhook to receive WhatsApp notifications when someone sends a message. The below is my code and it is not simply working giving server error or 405 method not allowed error, in the WhatsApp cloud api side it does not pass the validation point.
API
Route::GET('/webhook' , 'admin\InventoryInvoiceController#webhook')->name('webhook');
Controller
public function webhook() {
if($_SERVER['REQUEST_METHOD']=="GET"){
echo $_GET['hub_challenge']; //respond back hub_callenge key
http_response_code(200);
}else{
$data = json_decode(file_get_contents('php://input'), true);
error_log(json_encode($data)); //print inbound message
}
}
I have added this route to the exception so it's run with the need for CSRF validation. Error received on the Whatsapp API Cloud side:
The callback URL or verify token couldn't be validated. Please verify
the provided information or try again later.
What can I try next?
The problem was that I had not added my route to the CSRF exception which would invalidate the request send by Whatsapp cloud api. Make sure to do that before testing! Below is the code:
public function webhook(Request $request) {
$mode = $request->hub_mode;
$challenge = $request->hub_challenge;
$token = $request->hub_verify_token;
echo $challenge;
}
public function __construct() {
$this->middleware('auth:admin',
['except' => ['webhook', 'webhookpost']]
);
I am using the Microsoft Graph and I need to set up a webhook to receive changes to email and calendar events. I was able to get it working with my PHP Laravel application, but now that I am trying to subscribe to notifications, I am running into issues with validating the notificationUrl, which is pointing to a public server of mine.
The script for creating the webhook is returning the following error:
Client error: POST https://graph.microsoft.com/v1.0/subscriptions resulted in a 400 Bad Request response:
{
"error": {
"code": "InvalidRequest",
"message": "Subscription validation request failed. Response must ex (truncated...)
The truncated part I believe is
Subscription validation request failed. Must respond with 200 OK to this request.
Here is my code for creating the subscription:
$data = [
"changeType" => "created",
"notificationUrl" => "https://anatbanielmethod.successengine.net/office365/webhooks/events",
"resource" => "me/events",
"expirationDateTime" => "2018-12-20T18:23:45.9356913Z",
"clientState" => "secret",
];
$result = $graph->createRequest('POST', '/subscriptions')
->attachBody($data)
->execute();
and here is my method for my notificationUrl:
public function events()
{
//if validationToken exists return that to validate notificationUrl
if(isset($_REQUEST['validationToken'])){
return response($_REQUEST['validationToken'], 200)
->header('Content-Type', 'text/plain');
}
//process event normally for those that have already been validated
}
Once again this URL is public and live and I have tested it by using Postman to send it test posts and it is working fine. Also, I added this route to my VerifyCsrfToken middleware to allow a third party post to hit this URL.
Originally I set up a simple single page PHP script to test validating the notificationUrl and that simple script worked fine. It successfully validates Webhooks created that point to it. Here is that one page script code:
<?php
if(isset($_REQUEST['validationToken'])){
echo $_REQUEST['validationToken']; // needed only once when subscribing
} else {
//process like normal not a validation Token request...
}
}
So I would expect that the Laravel endpoint would work like the simple one page PHP script, and it is when I test both URLs in Postman, but the Laravel endpoint is not validating when Office365 attempts to validate it when creating a new webhook.
I have searched all over for help on this and read through all of the Microsoft developer documentation I can find on webhooks and these are some of the more helpful parts of the documentation but I am still not finding an answer to this issue:
https://learn.microsoft.com/en-us/graph/api/subscription-post-subscriptions?view=graph-rest-1.0
https://learn.microsoft.com/en-us/graph/webhooks#notification-endpoint-validation
Any ideas of this?
Thanks Marc! You were correct about the linefeed being the issue, I am still not sure where the line feed is coming from, some how Laravel appears to be adding it. Needless to say I found a solution by adding an "ob_clean();" right before returning the response. Below is my updated notificationUrl method:
public function events()
{
//if validationToken exists return that to validate notificationUrl
if(isset($_REQUEST['validationToken'])){
ob_clean();//this line is cleaning out that previously added linefeed
return response($_REQUEST['validationToken'], 200)
->header('Content-Type', 'text/plain');
}
//process event normally for those that have already been validated
}
It's odd that JakeD's answer requires the use of ob_clean(). here is my webhook controller method in my Laravel 5.7.x app:
use Illuminate\Http\Request;
public function webhook (Request $request) {
if (filled($request->input('validationToken'))) {
return response($request->input('validationToken'))
->header('Content-Type', 'text/plain');
}
// code to process the webhook after validation is complete
}
I don't see an extra linefeed character and the Microsoft Graph API subscription is validated and created.
<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.
I have a free installed instance of Cakephp 3.0 with several code examples, everything is working fine. Now I want to create a Auth component in ./src/Auth, concerning the documentation here
Thats my code:
<?php
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
class AlephAuthenticate extends BaseAuthenticate
{
public function authenticate(Request $request, Response $response)
{
// Do things for OpenID here.
// Return an array of user if they could authenticate the user,
// return false if not.
}
}
In AppContoller.php I initialize this component:
public function initialize() {
$this->loadComponent('Auth');
$this->Auth->config('authenticate', ['Aleph']);
}
Calling the application URL in the browser shows:
Fatal error: Declaration of App\Auth\AlephAuthenticate::authenticate() must be compatible with Cake\Auth\BaseAuthenticate::authenticate(Cake\Network\Request $request, Cake\Network\Response $response) in /var/www/art/src/Auth/AlephAuthenticate.php on line 7
Any idea whats wrong here?
Thanks!
Christoph
Please read the error message it is obvious. If you still have trouble understanding it just paste the error message into a search engine. This is the basic procedure for almost every error message if you don't know it and will probably always solve it. Guess it's in the php documentation as well.
Let me rephrase the error message for you: The method signatures don't match. To get rid off the error message make them match.