Composer with PHP Parse error: syntax error

Help with error, please:

Parse error: syntax error, unexpected ‘composer’ (T_STRING), expecting variable (T_VARIABLE) or ‘$’ in your code on line 3

<?php

$ composer require xmo/client

$basic  = new \xmo\client\credentials\basic();
$client = new \xmo\client($basic);

$cmd = 'https://rest.xmo.com/sms/json';

echo "aqui ----a $cmd \n";

$CAMPOS = array (
          'from' => 'aaa',
          'text' => 'test',
          'to' => '',
          'api_key' => '',
          'api_secret' => ''
          );

//Encode the array into JSON.
$JSONCAMPOS = json_encode($CAMPOS);

   $ch = curl_init();
   curl_setopt($ch,CURLOPT_URL, $cmd);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch,CURLOPT_POSTFIELDS, $JSONCAMPOS);

   $output = curl_exec($ch);
   curl_close($ch);

   echo "\n";
   echo $output;
   echo "\n";

?>

I admit this syntax looks off to me.

$ composer require xmo/client

Where are you getting the idea that is how it should be from?

EDIT
It just occurred to me what it looks like you’ve done. (seeing “composer” should have made it click before it did) Please correct me if I’m wrong.

You saw the code for a CLI command. i.e. the “$” representing the prompt, which you misinterpreted as being PHP.

AFAIK, packaged files need to be in place before runtime so they can be called in when needed.

2 Likes

Probably a clear example of copy&paste. Typically, that’s what you see when the author of the repo refers to using composer. So really, this is actually a Linux command that’s being “assumed” as a PHP code.

2 Likes

lol, ninja’d (in my defense my AM caffeine fix is just now starting to kick in) and it only just now occurred to me what that line was.

Do you know the proper way to use composer without using composer?

2 Likes

Just copy and paste from the Github readme.

1 Like

Thanks for the replies, but I’m not clear on the solution/remedy.
Any clarification will be appreciated.

It’s pretty clear already. Use composer to download that library. Once it’s downloaded, all you have to do is require the autoload.php file that gets included in the generated vendor folder.

Much thanks for your reply.
Not pretty clear to me, that’s why I’m here.
How would I use composer to download that library?
And what library?
Thanks again

google: composer tutorial

You have to first install composer which is super easy. Depending on which OS you have, you have to install it respectfully. Composer has step by step guides on their website for it. Once composer is installed. All you need to do is type into the terminal

composer require xmo/client

And that’s it. It’ll download it to the folder you are currently in. Though I am not sure where you got that code from because composer uses packagist.org to download from GitHub. I’ve done a quick search on both packagist and GitHub and I cannot find that library you are attempting to use.

Thanks for your reply.
My web host has already installed composer on the server where the php file resides.
So, what do I do next?

Did you not read what I wrote?

1 Like

Following the instructions your host gave you?

I don’t know how to execute a command on a remote server

Ask your host about how to ssh into the machine

I don’t think it should take 4 devs to tell you how to use the terminal. Really, you don’t even need a live server for this. The only actual thing that’s really needed are the composer files. Once you have them, you don’t even need to use composer again on a live site. You can do this on a local server and use the generated composer files and upload them to your live server.

Composer is just a faster way of including multiple repos without the need to use 50 lines of require. You can also download the repo from GitHub too if the service you are using has a GitHub repo for it. Otherwise, there’s really no legitimate reason to use composer if the service doesn’t include a legitimate repo. This means that it has to exist on packagist because like I said before, composer goes out to packagist and downloads it from there.

That isn’t completely true. You can set-up composer to pull files from anywhere. I’ve targeted github before to get the latest and greatest as well as fork things myself modify and point to my own repo.

If a php library is not a composer package it is probably very low quality. Libraries don’t have to be on package list though.

Dependency management is a fundamental, modern architectural construct. Nearly every language now has some form of dependency management. I would argue it is one of the most important things to learn after syntax. Autoloading also goes along with that.

What I meant was that packagist targets Github to download the repo from there. So it has to be a legitimate Github repo before you use composer.

Once again, not really. Composer can access a variety of source code repositories, not just github. https://getcomposer.org/doc/05-repositories.md#git-alternatives

Just saying.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.