I'm using Laravel for about one month and i wanted to try the guzzle module, to get last fm user infos
I've tried this request in my controller :
$client = new \GuzzleHttp\Client(['base_uri' => 'http://ws.audioscrobbler.com/']);
//$client = new \GuzzleHttp\Client();
$response = $client->get('2.0/?method=user.getinfo&user=rj&api_key=xxxxxxxxxx&format=json');
dd($response);
but i've just got this kind of things
Response {#190 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:11 [▼
"date" => array:1 [▶]
"server" => array:1 [▼
0 => "Apache/2.2.22 (Unix)"
]
"x-web-node" => array:1 [▼
0 => "www223"
]
"access-control-allow-origin" => array:1 [▼
0 => "*"
]
"access-control-allow-methods" => array:1 [▼
0 => "POST, GET, OPTIONS"
]
"access-control-max-age" => array:1 [▼
0 => "86400"
]
"cache-control" => array:1 [▼
0 => "max-age=60"
]
"expires" => array:1 [▶]
"content-length" => array:1 [▼
0 => "642"
]
"connection" => array:1 [▶]
"content-type" => array:1 [▶]
]
-headerLines: array:11 [▶]
-protocol: "1.0"
-stream: Stream {#181 ▼
-stream: :stream {#237 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
Could someone help me with an example or something :) ?
For body's contents:
echo $response->getBody();
Related
I am fetching data from database using stored procedure but getting below error
htmlspecialchars() expects parameter 1 to be string, array
Below is the controllers
public function index()
{
$id=2;
// $single_blog['user'] = UserRole::where('id',$id)->get();
$single_blog['user'] = DB::select("CALL FetchUserRoleWithId(".$id.")");
return view('home',$single_blog);
}
In view simple use only as
{{$user}}
If i use get() method then properly print $user data in blade file.
But using stored procedure getting below error
"htmlspecialchars() expects parameter 1 to be string, array"
Using stored procedure getting below data
array:1 [▼
"user" => array:1 [▼
0 => {#1237 ▼
+"id": 2
+"name": "SO"
+"status": "1"
+"created_by": null
}
]
]
Using get() method getting below data
array:1 [▼
"user" => Illuminate\Database\Eloquent\Collection {#1257 ▼
#items: array:1 [▼
0 => App\Models\UserRole {#1258 ▼
#fillable: array:4 [▶]
#connection: "mysql"
#table: "user_roles"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:8 [▼
"id" => 2
"name" => "SO"
"status" => "1"
"created_by" => null
"updated_by" => null
"deleted_at" => null
"created_at" => null
"updated_at" => null
]
#original: array:8 [▶]
#changes: []
#casts: array:1 [▶]
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#forceDeleting: false
}
]
#escapeWhenCastingToString: false
}
]
Anyone have idea how to display this data in blade file.
I am using Laravel 8 and Laravel Cashier for my subscriptions
When I tried to cancel the subscription, it returns
Call to a member function stripe() on null
when cancelling the subscription
Even the Auth::check() returns true
Here is my cancelSubscription method
public function cancelSubscription(Request $request){
$user = $request->user();
try{
// if(Auth::check()){
$user->subscription('My-Subscription-Name')->cancel();
// }
if($request->ajax()) {
return response([
'success' => true,
'data' => "Successfully subscribed"
], 200);
}
}catch (\Throwable $ex) {
Log::critical($ex);
if($request->ajax()) {
return response([
'success' => false,
'data' => "Server Error"
], 422);
}
}
}
Here is the code in api.php for the cancel subscription method
Route::post('/cancel-sub',[SubscriptionController::class, 'cancelSubscription'])->middleware('auth:api');
I've also tried this
public function cancelSubscription(Request $request){
$user = UserSubscription::where('stripe_id', $request->subId)->first(); //returns subscription id
try{
// if(Auth::check()){
$user->subscription('My-Subscription-Name')->cancel();
// }
if($request->ajax()) {
return response([
'success' => true,
'data' => "Successfully subscribed"
], 200);
}
}catch (\Throwable $ex) {
Log::critical($ex);
if($request->ajax()) {
return response([
'success' => false,
'data' => "Server Error"
], 422);
}
}
}
I've also tried
$user = $request->user();
$subscriptions = $user->subscriptions()->active()->first();
$subscriptions->cancel();
but it still doesn't work. Any answers will be appreciated.
I also did dd(auth()->user()->subscription('My-Subscription-Name')) and it returns true and here is the result
Laravel\Cashier\Subscription {#1293
#guarded: []
#with: array:1 [
0 => "items"
]
#casts: array:1 [
"quantity" => "integer"
]
#dates: array:4 [
0 => "created_at"
1 => "ends_at"
2 => "trial_ends_at"
3 => "updated_at"
]
#billingCycleAnchor: null
#connection: "mysql"
#table: "subscriptions"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:12 [
"id" => 32
"user_tbl_id" => 4697
"name" => "My-Subscription-Name"
"stripe_id" => "sub_xxx"
"stripe_status" => "active"
"stripe_price" => "price_xxx"
"quantity" => 1
"trial_ends_at" => null
"ends_at" => null
"expires_at" => "2022-10-13 20:38:54"
"created_at" => "2022-09-13 20:38:54"
"updated_at" => "2022-09-13 20:38:54"
]
#original: array:12 [
"id" => 32
"user_tbl_id" => 4697
"name" => "Choi-Nomi"
"stripe_id" => "sub_xxx"
"stripe_status" => "active"
"stripe_price" => "price_xxx"
"quantity" => 1
"trial_ends_at" => null
"ends_at" => null
"expires_at" => "2022-10-13 20:38:54"
"created_at" => "2022-09-13 20:38:54"
"updated_at" => "2022-09-13 20:38:54"
]
#changes: []
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [
"items" => Illuminate\Database\Eloquent\Collection {#1303
#items: array:1 [
0 => Laravel\Cashier\SubscriptionItem {#1301
#guarded: []
#casts: array:1 [
"quantity" => "integer"
]
#connection: "mysql"
#table: "subscription_items"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:9 [
"id" => 33
"subscription_id" => 32
"stripe_id" => "si_xxx"
"stripe_product" => "prod_xxx"
"stripe_price" => "price_xxx"
"quantity" => 1
"user_subscription_id" => null
"created_at" => "2022-09-13 20:38:54"
"updated_at" => "2022-09-13 20:38:54"
]
#original: array:9 [
"id" => 33
"subscription_id" => 32
"stripe_id" => "si_xxx"
"stripe_product" => "prod_xxx"
"stripe_price" => "price_xxx"
"quantity" => 1
"user_subscription_id" => null
"created_at" => "2022-09-13 20:38:54"
"updated_at" => "2022-09-13 20:38:54"
]
#changes: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#paymentBehavior: "allow_incomplete"
#prorationBehavior: "create_prorations"
}
]
#escapeWhenCastingToString: false
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#couponId: null
#promotionCodeId: null
#allowPromotionCodes: false
#paymentBehavior: "allow_incomplete"
#prorationBehavior: "create_prorations"
}
I did have a look at my old project and what I did was add another method on User Model, to also delete the subscription related entries on the database
public function cancelStripeSubscription( $name = 'My Subscription Name') {
$sub = $this->subscription($name);
if ( $sub ) {
DB::table('subscriptions')->where('id', $sub->id)->delete();
DB::table('subscription_items')->where('subscription_id', $sub->id)->delete();
if ( $sub->stripe_status != 'canceled' )
$sub->cancelNow();
}
}
so you can simply call auth()->user()->cancelStripeSubscription();
In you code however, you should first check if they have active subscription or subscribe to specific product by checking subscription status
something like this;
public function cancelSubscription(Request $request){
try{
if ( auth()->user()->subscribed('default') )
/*
or if ( $user->subscribedToProduct('prod_premium', 'default'))
or if ( $user->subscribedToPrice('price_basic_monthly', 'default'))
or
$sub = auth()->user()->subscription('My-Subscription-Name')
if ( $sub && $sub->stripe_status == 'canceled' )
*/
{
auth()->user()->subscription('My-Subscription-Name')->cancel();
} else {
return response([ 'error' => true, 'message' => 'You have no active subscription' ], 404);
}
.
.
}
.
.
}
Furthermore, verify if that subscripton your trying to cancel really is a subscription object and check if user is even have a subscription
public function cancelSubscription(Request $request){
return [
'request' => $request->all(),
'user' => auth()->user(),
'subscription' => auth()->user()->subscription('My-Subscription-Name'),
'subscriptions' => auth()->user()->subscriptions()
]
.
.
}
in controller I have array $checked
array:4 [▼
0 => "3"
1 => "4"
2 => "5"
3 => "8"
]
from base I want get second array, like this, but cannot:
$proservices = Proservice::whereIn('service_id', $checked)->get('product_id')->toArray();
it gives me 2 arrays:
array:3 [▼
0 => array:1 [▼
"product_id" => 14
]
1 => array:1 [▼
"product_id" => 7
]
2 => array:1 [▼
"product_id" => 14
]
]
help me to get array like this:
array:2 [▼
0 => "14"
1 => "7"
]
i am a beginner on laravel. i have problem on creating my POST API.
i want to use data retrieved from json POST from other application. but it always return null so i did some investigating on the request.
when i return dd($request), the result is
Illuminate\Http\Request {#43 ▼
#json: Symfony\Component\HttpFoundation\ParameterBag {#35 ▶}
#convertedFiles: []
#userResolver: Closure($guard = null) {#1241 ▼
class: "Illuminate\Auth\AuthServiceProvider"
this: Illuminate\Auth\AuthServiceProvider {#22 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
line: "105 to 107"
}
#routeResolver: Closure() {#1246 ▼
class: "Illuminate\Routing\Router"
this: Illuminate\Routing\Router {#26 …}
use: {▶}
file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
line: "655 to 657"
}
+attributes: Symfony\Component\HttpFoundation\ParameterBag {#45 ▼
#parameters: []
}
+request: Symfony\Component\HttpFoundation\ParameterBag {#35 ▼
#parameters: []
}
+query: Symfony\Component\HttpFoundation\InputBag {#51 ▼
#parameters: []
}
+server: Symfony\Component\HttpFoundation\ServerBag {#47 ▼
#parameters: array:23 [▶]
}
+files: Symfony\Component\HttpFoundation\FileBag {#48 ▼
#parameters: []
}
+cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▼
#parameters: []
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▼
#headers: array:6 [▼
"accept-encoding" => array:1 [▼
0 => "gzip,deflate"
]
"content-type" => array:1 [▼
0 => "application/json"
]
"content-length" => array:1 [▼
0 => "231"
]
"host" => array:1 [▼
0 => "localhost:8000"
]
"connection" => array:1 [▼
0 => "Keep-Alive"
]
"user-agent" => array:1 [▼
0 => "Apache-HttpClient/4.5.5 (Java/12.0.1)"
]
]
#cacheControl: []
}
#content: """
{\n
\t"id_employee" = "4",\n
\t"status" = "1",\n
\t"latitude" = "3.4141414",\n
\t"longitude" = "98.444444",\n
\t"timestamp" = "2020-10-11 09:58:00.0000000",\n
\t"approval" = "1",\n
\t"message" = "this is message",\n
\t"picture_reference" = "this is pic"\n
}
"""
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/api/Attendance"
#requestUri: "/api/Attendance"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-preferredFormat: null
-isHostValid: true
-isForwardedValid: true
-isSafeContentPreferred: null
basePath: ""
format: "html"
}
}
when i return $request->getcontent() the result is
{ "id_employee" = "4", "status" = "1", "latitude" = "3.4141414", "longitude" = "98.444444", "timestamp" = "2020-10-11 09:58:00.0000000", "approval" = "1", "message" = "this is message", "picture_reference" = "this is pic" }
but when i return $request->all() is says
<Empty JSON content>
whenever i use $request->id_employee or any other data, it always return null. when i put parameter on the URL like "?id_employee=1", $request->id_employee will return 1. i do not want to put the data on the URL, so how do i get all the content on $request?
edit:
i already tried
$id_employee = $request->id_employee;
$id_employee = $request->input("id_employee")
$id_employee = $request->input("content.id_employee")
and already confirm that $request->isjson() return 1
What version of Laravel do you have? You should be able to decode the JSON content, like this:
$content = $request->getContent()
$data = json_decode($content, true)
dd(data)
Check that the JSON is valid though - the example that you posted in your original questions had = where the : should normally go! The JSON needs to be valid and if the request has the correct Content-Type header then your call to request->input("employee_id") should work. That it doesn't makes me wonder if the JSON is well-formatted so definitely check that as well.
I am new to Laravel and Yajra datatable. I have developed a web application (In windows XAMPP) and everything works perfectly until I upload my project in shared hosting (UNIX) recently, the datatable unable to load the view and throwing the error message.
The error message I get is:
ErrorException in Request.php line 38:
Undefined index: value
in Request.php line 38
at HandleExceptions->handleError('8', 'Undefined index: value', '/home/posgb/public_html/boatMain/vendor/yajra/laravel-datatables-oracle/src/Request.php', '38', array()) in Request.php line 38
I have compared DD results of my query builder output in both my machine and server and found that the "value" attribute was missing from the "search" array.
My query:
$query = DB::table('item_subcat')
->leftJoin('item_customized', 'item_subcat.subcatID', '=', 'item_customized.subcatID')
->join('item_cat', 'item_subcat.itemCatID', '=', 'item_cat.itemCatID')
->select(array('item_subcat.subcatID', 'item_subcat.itemCode', 'item_subcat.itemName', 'item_cat.itemCatName', 'item_customized.customize_name', DB::raw('IF(item_subcat.is_categorize = "0", item_subcat.itemPrice, item_customized.price) AS ITEMPRICE'), 'item_subcat.is_activate', 'item_customized.itemCustomID'));
dd($query);
DD results in my pc:
#parameters: array:7 [
"draw" => "1"
"columns" => array:7 [
0 => array:5 [ …5]
1 => array:5 [ …5]
2 => array:5 [ …5]
3 => array:5 [ …5]
4 => array:5 [ …5]
5 => array:5 [ …5]
6 => array:5 [ …5]
]
"order" => array:1 [
0 => array:2 [ …2]
]
"start" => "0"
"length" => "10"
"search" => array:2 [
"value" => "" /*This is the missing value */
"regex" => "false"
]
"branch" => "ALL"
]
}
DD result in my server:
+request: ParameterBag {#41
#parameters: array:7 [
"draw" => "1"
"columns" => array:7 [
0 => array:5 [ …5]
1 => array:5 [ …5]
2 => array:5 [ …5]
3 => array:5 [ …5]
4 => array:5 [ …5]
5 => array:5 [ …5]
6 => array:5 [ …5]
]
"order" => array:1 [
0 => array:2 [ …2]
]
"start" => "0"
"length" => "10"
"search" => array:1 [
"regex" => "false"
]
"branch" => "ALL"
]
}
Please can advise me where should I look into the missing "value" in array, as it cause me unable to generate the datatable. TYVM for those spent times to look at this.
The reason was due to PHP.ini settings in my server which disallow null value in GET and POST request by default.