Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Konstruktionsplan's avatar

cURL-Massacre 🩸

Hello!

So slowly I'm getting desperate. I have a simple cURL command. There is a URL and after "-u" a "CONSUMER_KEY" and a "CONSUMER_SECRET".

Via the terminal it works wonderfully. How do I implement this in PHP with Laravel. The documentation just tells me nothing and helps 0.

So I hope someone here knows a simple method to fire a simple command and return its response.

Thanks.

0 likes
7 replies
Konstruktionsplan's avatar

As said. The site did not help me because there are no exampels that are close to reality. At least as far as this problem is concerned.

Ishra's avatar

What client are you using in Laravel? The http client that jlrdw linked or guzzle?

-u is the the option to specify username and password

To do this with http client, the withBasicAuth should be the same as -u on the commandline

https://laravel.com/docs/8.x/http-client#authentication

// Basic authentication...
$response = Http::withBasicAuth('[email protected]', 'secret')->post(...);

With guzzle you can read about the auth option

https://docs.guzzlephp.org/en/stable/request-options.html#auth

$client->request('GET', 'url...', ['auth' => ['username', 'password']]);

If you just want to run your command directly you can use the exec function

https://www.php.net/manual/en/function.exec.php

But i would highly recommend you to do http requests using laravel's built in http client, guzzle or atleast the curl functions in php https://www.php.net/manual/en/book.curl.php

I recommend to use one of the mentioned abstractions over curl_* functions, your life will be much easier.

1 like
Konstruktionsplan's avatar

Hi, big thank you for the explanation! I have now tried it with BasicAuth. Unfortunately this does not work. But I get correct results via the conosole.

curl https://playground-wordpress.site/wp-json/wc/v3/orders -u ck_3345f111b028e182d49feeca982815c6b3dacebe:cs_eaa8618793277f5eb4c17625dace0969bdd50a81

The keys don't matter for now, since it's all local.

My try with Laravel:

$response = Http::withBasicAuth(
     'cs_eaa8618793277f5eb4c17625dace0969bdd50a81', 'ck_3345f111b028e182d49feeca982815c6b3dacebe')
    ->get('http://playground-wordpress.site/wp-json/wc/v3/orders');

dd($response);

Output:

Illuminate\Http\Client\Response {#494 â–¼
  #response: GuzzleHttp\Psr7\Response {#529 â–¼
    -reasonPhrase: "Not Found"
    -statusCode: 404
    -headers: array:6 [â–¶]
    -headerNames: array:6 [â–¶]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#527 â–¶}
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar {#508 â–¶}
  +"transferStats": GuzzleHttp\TransferStats {#530 â–¶}
Ishra's avatar

Take note of the error you are getting, does not appear to have anything to do with the authentication.

If you try an invalid username/password on purpose, do you still get statusCode 404? If you get something else, like 403, that means that the authentication (what -u does) is solved, and that we now have another problem to solve.

I see that you are using https:// in the terminal, but you are using http://

could this be the problem?

Anyway, the problem seems to be that the server of playground-wordpress.site is responding that it cannot find the url you are trying to reach, are your laravel installation and this wordpress site on the same computer?

When you are using the terminal, is that on the same computer as your laravel installation?

1 like
Konstruktionsplan's avatar

Laravel and WooCommerce runs on the same local machine. But your post 🖤 got me thinking. Yes, it may be that the terminal can fire a successful cURL statement to the local environment, but I shouldn't assume that an application can as well. I'll push the store to a live server and would test it again and get back to you if necessary.

Thanks for this thought! :)

1 like
Konstruktionsplan's avatar

Now I have pushed everything up and get at least already halfway something "right". Oddly enough, it now tells me that I am unauthorized. With terminal it now also no longer works.

But the keys are stored correctly.

Illuminate\Http\Client\Response {#494 â–¼
  #response: GuzzleHttp\Psr7\Response {#529 â–¼
    -reasonPhrase: "Unauthorized"
    -statusCode: 401
    -headers: array:15 [â–¼
      "Date" => array:1 [â–¶]
      "Server" => array:1 [â–¶]
      "X-Powered-By" => array:1 [â–¶]
      "X-Robots-Tag" => array:1 [â–¶]
      "Link" => array:1 [â–¶]
      "X-Content-Type-Options" => array:1 [â–¶]
      "Access-Control-Expose-Headers" => array:1 [â–¶]
      "Access-Control-Allow-Headers" => array:1 [â–¶]
      "Vary" => array:1 [â–¶]
      "Content-Type" => array:1 [â–¶]
      "X-Varnish" => array:1 [â–¶]
      "Age" => array:1 [â–¶]
      "Via" => array:1 [â–¶]
      "Content-Length" => array:1 [â–¶]
      "Connection" => array:1 [â–¶]
    ]
    -headerNames: array:15 [â–¶]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#527 â–¶}
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar {#508 â–¶}
  +"transferStats": GuzzleHttp\TransferStats {#530 â–¶}

Edit:

In the WooCommerce description is somehow still passed the "wp_api" and "version". Do I have to (and can I) pass that via Laravel as well?

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'https://example.com',
    'consumer_key',
    'consumer_secret',
    [
        'wp_api' => true,
        'version' => 'wc/v3'
    ]
);

Please or to participate in this conversation.