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
  • 741,623
  • 53
  • 500
  • 612
  • You don't need `.` if you're substituting the variables inside the string. – Barmar Nov 24 '21 at 04:19
  • `"{$baseUrl}{$api}&interval=1d,30d&convert=USD&per-page=100&page=1"` – Barmar Nov 24 '21 at 04:20
  • So it's just like this ??? $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" ???? –  Nov 24 '21 at 04:21
  • If the variables are outside the quotes, you have to use `.` to concatenate them. If they're inside the quotes, you don't. – Barmar Nov 24 '21 at 04:21
  • Barmar - I get ErrorException Undefined variable $baseUrl –  Nov 24 '21 at 04:22
  • The variable is declared outside the function. Either pass it as a parameter or use `global $baseUrl;` – Barmar Nov 24 '21 at 04:23
  • What does it look like if I want to use global ? –  Nov 24 '21 at 04:29
  • See https://stackoverflow.com/questions/2531221/giving-my-function-access-to-outside-variable – Barmar Nov 24 '21 at 04:57

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,493
  • 7
  • 34
  • 86
  • Hi, for learning purpose, could you show me how to set these variables globally and access from the function ? using global ? –  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.... –  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. –  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 ? –  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 –  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