Get data from put request Yii2 - php

I do rest API. To update data using a PUT request
http://train-basic/stations/21?name=tt
Try get data:
$request = Yii::$app->request;
$request = $request->post();
$name = $request["name"];
dump($name);
As a result, I get null. How to fix it?

To get data sent in request body by PUT or PATCH request, you should use getBodyParam() or getBodyParams()
$request = Yii::$app->request;
// returns all parameters
$params = $request->getBodyParams();
// returns the parameter "id"
$param = $request->getBodyParam('id');
https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests#request-parameters

Data from PUT download like POST
$request = Yii::$app->request;
$id = $request->get('id');
$name = $request->get('name');
$days = $request->get('days');

Related

How to get big record data from external API in laravel 5.7

I want to get 28,000 records data from external API.
For this, I use nusoap_client. However, when I test it I don't get any data and I don't get any error.
This is my function:
public function get_all(){
$url = "http://xxx.xx.x.xx:xxx/services/Pending?wsdl";
$client = new \nusoap_client($url, 'wsdl');
$pDate = isset($_GET["Date"]) ? $_GET["Date"] : '26/2/2017 01:01:01';
$operation = 'getPendingData';
$param1 = array(
'Date' => $Date,
);
$result = $client->call($operation, $param1);
print_r($result);
exit();
}
In the API I must get data starting from 26/02/2017 01:01:01. When I test it I don't get any data. When I changed date to 27/02/2017 01:01:01 I got 759 records data.
How to fix this?

AuthnetWebhook package signature key validation

I got SIGNATURE_KEY from my authorize.net merchant interface. I am using AuthnetJson Package. Should i have to convert 128 hexadecimal SIGNATURE_KEY to binary? If answer is yes then i did so but my code never execute inside if ($webhook->isValid()){// code never execute execute}. What i am doing wrong?
$webhook = new AuthnetWebhook('services.authorize.signature', $payload);
if ($webhook->isValid()) {
// Get the transaction ID
$transactionId = $webhook->payload->id;
// Here you can get more information about the transaction
$request = AuthnetApiFactory::getJsonApiHandler('services.authorize.login', 'services.authorize.key');
$response = $request->getTransactionDetailsRequest(array(
'transId' => $transactionId
));
$user = User::find(1);
$user->notify( new PasswordResetSuccess($response));
/* You can put these response values in the database or whatever your business logic dictates.
$response->transaction->transactionType
$response->transaction->transactionStatus
$response->transaction->authCode
$response->transaction->AVSResponse
*/
}
Edit:
<?php
namespace App\Http\Controllers\Api\Anet;
use Illuminate\Http\Request;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
use App\Http\Controllers\Controller;
use JohnConde\Authnet\AuthnetWebhook;
use App\Notifications\PasswordResetSuccess;
use App\Models\User;
use Log;
use \stdClass;
use App\Models\Anet;
class WebhookController extends Controller
{
public function webhook(Request $request){
$headers = getallheaders();
$payloadraw = file_get_contents("php://input");
$payloadEncoded = json_encode($payloadraw);
$payloadDecoded = json_decode($payloadraw);
$type = gettype($payloadraw);
$webhook = new AuthnetWebhook('xxxxx8EF4B4186A3BC745B70637EA1Fxx091E1DD0706BF9A9D721982B882BE54192BD1BBCEAFC0415DF06E6xxxxxxxxx',$payloadEncoded, $headers);
if ($webhook->isValid()) {
// Get the transaction ID
$transactionId = $webhook->payload->id;
// Here you can get more information about the transaction
$request = AuthnetApiFactory::getJsonApiHandler('AUTHNET_LOGIN','AUTHNET_TRANSKEY');
$response = $request->getTransactionDetailsRequest(array('transId' => $transactionId));
$anet = new Anet();
$anet->notification = $payloadraw ;
$anet->payload = $payloadDecoded ;
$anet->type = $type ;
$anet->transaction_type = $response->transaction->transactionType;
$anet->transactions_status = $response->transaction->transactionStatus;
$anet->auth_code = $response->transaction->authCode;
$anet->avs_response = $response->transaction->AVSResponse;
$anet->save();
}else{
$anet = new Anet();
$anet->notification = $payloadEncoded ;
$anet->payload = $payloadDecoded ;
$anet->type = $type ;
$anet->transactions_status = '401';
$anet->save();
}
}
}
You do not need to convert it to binary. It's value as displayed in the Authorize.Net interface is how it should be used in your code:
Example:
$webhook = new AuthnetWebhook('14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE1752CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E', $payload);
Or, if you use the config.inc.php configuration file from your library:
defined('AUTHNET_SIGNATURE') || define('AUTHNET_SIGNATURE', '14FE4A2385812E980CCF97D177F17863CE214D1BE6CE8E1E894487AACF3609C1A5FE1752CB4A002C634B84E397DC8A218E1A160BA7CAB7CBE4C05B35E9CBB05E');
and in your code:
$webhook = new AuthnetWebhook(AUTHNET_SIGNATURE, $payload);
Without referencing the rest of your logic, the reason your if statement evaluates to untrue is that you are json encoding data that is already json. When you call getallheaders() the content-type is already defined as json. So replace this:
$headers = getallheaders();
$payloadraw = file_get_contents("php://input");
$payloadEncoded = json_encode($payloadraw);
$payloadDecoded = json_decode($payloadraw);
$type = gettype($payloadraw);
with this:
$headers = getallheaders();
$payload = file_get_contents("php://input");
and this:
$webhook = new AuthnetWebhook($signature,$payload, $headers);
if ($webhook->isValid()) {
//logic goes here
}
Will evaluate to true, and any valid logic contained in the conditional will be executed. I recommend testing the above code to validate that it works (it does) before adding additional logic. You could create a simple log file like this:
$dump = print_r($payload,true);
$fp = file_put_contents( '
test.log', $dump );
And if your directory has a file called test.log after a single webhook is delivered, you know you have a baseline to go from. If there is invalid logic in the rest of your if statement, it may break the entire thing.
And to answer your first question, which has already been answered correctly, do not convert signature key to binary. So $signature in the above code is the signature key exactly as given to you by authorize.

Modification of database outputs from model and passing to view laravel 5.2

my RESTapi is outputting following json string
{"chat":[{"id":100,"chat_id":38,"created_at":"2016-09-07 08:48:17","updated_at":"2016-09-07 08:48:17","messageContent":"Hi there","sender":"client"},{"id":101,"chat_id":38,"created_at":"2016-09-07 08:48:29","updated_at":"2016-09-07 08:48:29","messageContent":"hello sir","sender":"admin"},{"id":102,"chat_id":38,"created_at":"2016-09-07 09:14:24","updated_at":"2016-09-07 09:14:24","messageContent":"test","sender":"client"},{"id":103,"chat_id":38,"created_at":"2016-09-07 09:16:06","updated_at":"2016-09-07 09:16:06","messageContent":"test","sender":"client"}],"currentChatId":38,"senderName":"Client name"}
Notice the sender column. These are all coming from db. Now I am writing an algorithm which checks this and finds the sender name to the view instead of just sender type.
I am having problem regarding this..any suggestion?
Here is the controller function
public function getAllChat(Request $request)
{
// $chatList = chatMessage::where();
$clientId = $request->session()->get('userId');
//current chat id7
$currentChatId = $request->session()->get('currentChatId');
//find who sent it admin/client
$chatMessageList = chatMessage::where('chat_id',$currentChatId)->get();
//sender
foreach($chatMessageList as $cht)
{
//find out sender
$sender = $cht->sender;
if($sender=="client")
{
$chtP = chatParticipants::find($currentChatId)->first();
$clientId = $chtP->client_id;
//find client name
$client = Client::find($clientId);
$ch->sender = $client->name;
}
elseif($sender=="admin")
{
$chtP = chatParticipants::find($currentChatId)->first();
$adminId = $chtP->admin_id;
//find client name
$admin = Admin::find($clientId);
$name = $admin->name;
$ch->sender = $admin->name;
}
}
return response()->json([
"chat"=> $chatMessageList,
"currentChatId" => $currentChatId,
"senderName"=>$name
]);
}
Error : Creating default object from empty value empty value
You don't have $ch variable defined anywhere - I guess you wanted to use $cht variable instead.
Replace
$ch->sender = $client->name;
with
$cht->sender = $client->name;
and
$ch->sender = $admin->name;
with
$cht->sender = $admin->name;
It was a typo.
$cht->sender=$name;
instead of
$ch->sender=$name;

Symfony 2 set Request content and retrieve it

I am working in Symfony2 and I want to se the content of a Request with a JSON string and use i.e.: $request->get('name') to access the content.
JSON string:
$string = '{
"name":"Bob",
"surname":"White",
"email":"bobwhite#gmail.com",
"nationality":"",
}';
$request = new Request ($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = $string);
var_dump($request->get('name'));die;
To me the above is a valid way but the var dump gives me null... can anyone see where I m going wrong here...?
You want something like this?
use Symfony\Component\HttpFoundation\Request;
$input = '{
"name":"Bob",
"surname":"White",
"email":"bobwhite#gmail.com",
"nationality":""
}';
$data = json_decode($input, true);
$request = new Request (array(), $data);
var_dump($request->request->get('name'));
die;

HTTP PUT Parameter

I create a rest-webservice with the php framework "tonic".
I have a User Class and handle it with the library.
According to CRUD i use HTTP_PUT to UPDATE the User:
function put($request) {
$response = new Response($request);
$split = explode ('&',$request);
$para = array();
foreach($split as $i) {
$names = explode('=',$i);
$para[$names[0]] = $names[1];
}
$response->body = var_dump($para);
return $response;
}
My Question is how do I access the calling parameters?
At the moment I parse it manually into an array.
PHP will not translate a classic "application/x-www-form-urlencoded" request into $_POST / $_GET if the method is PUT (and there is no $_PUT).
So if you use this content type you have to parse the query string manually:
<?php
$putdata = fopen("php://input", "r");
$para = parse_str($putdata);
http://www.php.net/manual/en/features.file-upload.put-method.php

Categories