0

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

$baseUrl = "https://api.nomics.com/v1/currencies/ticker?key=";
$api = "df87f0731def2f83a8539a2735b4c31ee";


class ProjectController extends Controller
{
    public function getData(Request $request){
        $responce = Http::get("https://api.nomics.com/v1/currencies/ticker?key=df87f0731def2f83a8539a2735b4c31ee2b6f0b5&interval=1d,30d&convert=USD&per-page=100&page=1");
        return view('index', ['responce' => $responce->json()]);
    }
}
//curl "https://api.nomics.com/v1/currencies/ticker?key=df87f0731def2f83a8539a2735b4c31ee&interval=1d,30d&convert=USD&per-page=100&page=1"

I am trying to simplify string inside of get function by doing.

"{$baseUrl}{$api} . &interval=1d,30d&convert=USD&per-page=100&page=1"

or

$baseUrl . $api . "&interval=1d,30d&convert=USD&per-page=100&page=1"

It seems not working. Is there a recommendation of doing that ? I just started learning PHP. Thank you.

Barmar
  • 669,327
  • 51
  • 454
  • 560
parapara
  • 3
  • 3

1 Answers1

0
  • You can format strings with sprintf().
  • You can escape double quotes naturally in PHP, the braces just help for readability.
  • You can concatenate single quotes and functions with .

PSB an example of each usage.

// Using format string functions
Http::get(sprintf('%s%s&interval=1d,30d&convert=USD&per-page=100&page=1', $baseUrl, $api));

// Escape - {} are not needed but increases readability
Http::get("{$baseUrl}{$api}&interval=1d,30d&convert=USD&per-page=100&page=1");

// Concatenate
Http::get($baseUrl . $url . '&interval=1d,30d&convert=USD&per-page=100&page=1');

You should also remember the scopes of your project and declare the variables correctly.

class ProjectController extends Controller
{
    private string $baseUrl = 'https://api.nomics.com/v1/currencies/ticker?key=';
    private string $api     = 'df87f0731def2f83a8539a2735b4c31ee';

    public function getData(Request $request)
    {
        $response = Http::get(sprintf('%s%s&interval=1d,30d&convert=USD&per-page=100&page=1', $this->baseUrl, $this->api));
        return view('index', compact('response'));
    }
 }
Jaquarh
  • 6,423
  • 5
  • 27
  • 66
  • Hi, for learning purpose, could you show me how to set these variables globally and access from the function ? using global ? – parapara Nov 24 '21 at 04:32
  • [You can read the documentation](https://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.global), maybe? – Jaquarh Nov 24 '21 at 04:33
  • "$GLOBALS['baseUrl'] $GLOBALS['api'] &interval=1d,30d&convert=USD&per-page=100&page=1" seems not working.... – parapara Nov 24 '21 at 04:36
  • The documentation simply states to just use `global $baseUrl, $api` inside the scope you want to use the variables. [See it working here.](https://3v4l.org/7A9N4) – Jaquarh Nov 24 '21 at 04:38
  • Oh I got it thanks. but I think it's not good way to use in this case. – parapara Nov 24 '21 at 04:41
  • I address your issue in my answer by using [class properties](https://www.php.net/manual/en/language.oop5.properties.php) which are declared inside of the class scope making them accessible using `$this` inside your class functions. You're going completely off the OP subject here. – Jaquarh Nov 24 '21 at 04:42
  • I see GLOBALS in the documentation but not global. are they different ? – parapara Nov 24 '21 at 04:44
  • Accessing global variable inside function: The ways to access the global variable inside functions are: Using global keyword Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable. This array is also accessible from within functions and can be used to perform operations on global variables directly. This helpled – parapara Nov 24 '21 at 04:47
  • Yes, [which works perfectly fine](https://3v4l.org/C5mAi), also? What exactly are you asking now? – Jaquarh Nov 24 '21 at 04:49