I am trying to get my webhooks header (Woocommerce webhook),
I am retrieving the body with file_get_contents('php://input'), although this only gives the body according to http://php.net/manual/en/wrappers.php.php
I also found this thread: link, but I can't figure it out.
Is there any other function that gives me back the header?
My function looks like this:
public function webhook(Request $request) {
$json = file_get_contents('php://input');
Storage::disk('local')->put('file.txt', $json);
}
Edit: Other things I tried:
public function webhook(Request $request) {
$json = file_get_contents('php://input');
$headers = getallheaders();
Storage::disk('local')->put('file.txt', $headers['Content-Name']);
}
This sets the webhook to "Disabled", I suppose this throws an error for some reason.
apache_request_headers is not changing the status to "Disabled" but is returning an empty file.txt
Use getallheaders():
This function exists for the sole purpose of retrieving request headers:
$headers = getallheaders();
var_dump($headers['Content-Name']);
Is there any other function that gives me back the header?
Assuming you use Apache, then: http://php.net/apache-request-headers
For the ones who might be facing the same problem in the future, I found the following solution:
public function webhook(Request $request) {
$json = file_get_contents('php://input');
Storage::disk('local')->put('file.txt', $json);
Storage::disk('local')->put('request.txt', Request::header('x-wc-webhook-source'));
}
Main url solution: Link
Related
I have a function developed by PHP that at the moment all I want it to do is to return the value of the variable $_POST['token']
I tried:
public function actionGetuserbytoken() {
$data = json_decode(file_get_contents("php://input"), TRUE);
$id = $data['token'];
return $id;
}
Or I also just tried:
public function actionGetuserbytoken() {
return $_POST['token'];
}
I tried doing the POST with Insomnia to check what is going on:
I feel this is a very absurd question but I can't understand why I can't get the value of the POST in either of the two ways.
The php://input stream can only be read once per request. Yii is likely reading the payload before you can, which means that the body is empty when you read the data.
Instead of using php://input, try the following:
$data = json_decode(Yii::app()->request->getRawBody(), true);
I am making rest API with Yii Plus when I am trying to print_r request (using Postman) it's empty, can anyone let me know what I am doing wrong.
<?php
namespace frontend\controllers;
use Yii;
use yii\rest\Controller;
class ApiController extends Controller
{
Const APPLICATION_ID = 'ASCCPE';
private $format = 'json';
public function actionUserRegister()
{
$request = \Yii::$app->request->post(); $post = (file_get_contents("php://input"));
print_r($request);
die('sdw');
}
}
Output
You are not trying to print request. You are trying to print post data, but you are not sending any post data by your request.
The \Yii::$app->request->post(); returns data from $_POST array. This array is filled from request body only for data that have been sent in form-data or x-www-form-urlencoded format.
In postman click open the body part of request, select one of the two mentioned formats and fill the data you want to send.
If you want to use other format for request, like json or xml, you have to read it from php://input. You already have it in your code:
$post = (file_get_contents("php://input"));
So try to print the $post instead of $request variable. But you still need to fill the body part of request in postman.
The params you've set in postman are the GET params. Those are part of request's url. You can get them for example like this:
$request = \Yii::$app->request->get();
You are returning with the message in the die function.
Instead of this you can try this way:
die(print_r($request, true));
Example:
public function actionCreate()
{
$request = Yii::$app->request->post();
die(print_r($request, true));
}
better:
return print_r($request, true);
Example:
public function actionCreate()
{
$request = Yii::$app->request->post();
return print_r($request, true);
}
better:
// include the VarDumper class
\Yii::info(VarDumper::dumpAsString($request));
// output will be located in your app.log file
More info on the print_r function
Example:
public function actionCreate()
{
return Yii::$app->request->getBodyParams();
}
public function actionStripeHook() {
$this->layout = '//layouts/empty';
try {
Stripe::setApiKey(Yii::app() - > params['secret_key']);
$postdata = file_get_contents("php://input");
$event = json_decode($postdata);
$input = #file_get_contents("php://input");
var_dump($input);
$event_json = json_decode($input);
}
}
In Stripe Webhook set this Url,But in this response i got always empty whats wrong in my code.
If you read the note on this page:
http://php.net/manual/en/wrappers.php.php
You will see that in many versions of PHP php://input can only be read once, and will be empty the second time it is read. If you look closely at your code you will notice that you are reading php://input twice. Make sure and only read it once. That is probably your problem.
I'd like to generate a JavaScript file for JSONP data exchange.
All fine, but I need / want to set the header as :
header("Content-Type: text/javascript");
or
header("Content-Type: application/javascript");
Is this possible in a response from a controller in Laravel 4, or do I need to create a view and set the header with PHP?
I'd like to output something like:
var obj = JSON.parse('{"item1":"value1","item2":"value2"}');
// then do whatever with the object
$('#somediv').html(obj.item1);
Thanks for your help in advance
Okay, looks like I have to answer my question myself :-). Thanks to #terrylow for trying though.
Here is the way to change the header of my response using my function in my controller
public function javascriptResponse(){
$statusCode = 200;
$content = "var obj = JSON.parse('{\"item1\":\"value1\",\"item2\":\"value2\",\"some\":\"whoaevaNew\"}');";
$response = Response::make($content, $statusCode);
$response->header('Content-Type', 'application/javascript');
return $response;
}
variable content can also be filled with a view:
$content = View::make('tools/jsonp_resonse'); // also possible with view
Hope that will help someone...
you can use this method provided by laravel
return Response::jsonp($callback, $data,$status, $header);
Im trying to use the GiantBomb api to query video games, and currently when I enter the URL into a browser, it works just fine. The Json data shows up.
Heres an example url..
http://www.giantbomb.com/api/search/?api_key=83611ac10d0dfghfgh157177ecb92b0a5a2350c59a5de4&query=Mortal+Kombat&format=json
But when I try to use my php wrapper that Im just starting to build, it returns html??
Heres the start of my wrapper code....(very amateur for now)
You'll notice in the 'request' method, Ive commented out the return for json_decode($url), because when I uncomment it, the page throws a 500 error??? So I wanted to see what happends when I just echo it. And it echos an html page. Surely it should just echo what is shown, when you just enter that url into the browser, no?
However...if I replace the url with say a GoogleMap url, it echoes out Json data just fine, without using json_decode. Any ideas as to wahts going on here????
class GiantBombApi {
public $api_key;
public $base_url;
public $format;
function __construct() {
$this->format="&format=json";
$this->api_key = "83611ac10d0d157177ecb92b0a5a2350c59a5de4";
$this->search_url = "http://www.giantbomb.com/api/search/?api_key=".$this- >api_key."&query=";
}
public function search($query){
$query = urlencode($query);
$url = $this->search_url.$query.$this->format;
return $this->request($url);
}
public function request($url) {
$response = file_get_contents($url);
echo $response;
//return json_decode($response, true);
}
}
//TESTING SECTION
$games = new GiantBombApi;
$query = $_GET['search'];
echo $games->search($query);
I ran a few requests through Postman and it seems that the api looks at the mime-type as well as the query string. So try setting a header of "format" to "json".