Missing Class definition?

I have this code, provided by someone else:
<?php

$request = new HttpRequest();
$request->setUrl('https://api.ltl.xpo.com/rating/1.0/ratequotes');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'format' => 'XML'
));

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/xml',
  'authorization' => 'Bearer 5ce5e6e3304fd4e069b1c00f1c1f9bab'
));

$request->setBody('<?xml version="1.0" encoding="UTF-8"?>
<GetXpoLtlRateQuoteRqst xmlns="http://www.ltl.xpo.com/API/LTLRatingAPI">
<shipmentInfo>
    <paymentTermCd>P</paymentTermCd>
    <accessorials>
        <accessorialCd>DNC</accessorialCd>
        <accessorialCd>GUR</accessorialCd>
    </accessorials>
    <commodity>
        <grossWeight>
            <weight>500</weight>
            <weightUom>lbs</weightUom>
        </grossWeight>
        <nmfcClass>100</nmfcClass>
        <hazmatInd>false</hazmatInd>
    </commodity>
    <shipmentDate>2018-11-19</shipmentDate>
    <shipper>
        <address>
	<acctInstId>117258878</acctInstId>
            <postalCd>93727</postalCd>
        </address>
    </shipper>
    <consignee>
        <address>
            <postalCd>92027</postalCd>
        </address>
    </consignee>
</shipmentInfo>
</GetXpoLtlRateQuoteRqst>
');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

Open in new window


It fails with: Fatal error: Class 'HttpRequest' not found in /home/lakoshva/public_html/freight_test.php on line 3

Doesn't there have to be something like "require once xxxxxxxxxxxxxclass.php"?

Thanks
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
Possibly, yes. It all depends on what framework you're using, if any. Some frameworks have some special lines of code to include at the top of your script in order to "initialize everything", which includes database connections, class definitions, etc... Other frameworks just have you add the require() or include() at the top of the page.

There are also auto-class loaders that will load up the right file if the class doesn't exist yet (you just tell the loader where to look for the class files).
gr8gonzoConsultantCommented:
Doh. I just looked at your code. You're talking about the PECL extension for HTTP, which you can get from here:

http://pecl.php.net/package/pecl_http

Once you install that, your code should work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
gr8gonzo,

Thanks for your help.

I downloaded the latest version (it took it to 7Zip). See attached. Do I extract & upload all files to the web server or??.

Thanks,

Richard
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

gr8gonzoConsultantCommented:
Usually you just run the command on the server:

pecl install <the extension you want to install>

If you're not in root and need to be, then you'd prefix the command with sudo, like this:

sudo pecl install pecl_http
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
I had the host install in on the server. It produces this:

Warning: Missing argument 1 for HttpRequest::__construct(), called in /home/lakoshva/public_html/freight_test.php on line 3 and defined in /home/lakoshva/public_html/HttpRequest.php on line 5

Fatal error: Call to a member function setUrl() on null in /home/lakoshva/public_html/HttpRequest.php on line 11

The current code (in case I made changes) is
<?php
require 'HttpRequest.php';
$request = new HttpRequest();
$request->setUrl('https://api.ltl.xpo.com/rating/1.0/ratequotes');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'format' => 'XML'
));

$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'content-type' => 'application/xml',
  'authorization' => 'Bearer 5ce5e6e3304fd4e069b1c00f1c1f9bab'
));

$request->setBody('<?xml version="1.0" encoding="UTF-8"?>
<GetXpoLtlRateQuoteRqst xmlns="http://www.ltl.xpo.com/API/LTLRatingAPI">
<shipmentInfo>
    <paymentTermCd>P</paymentTermCd>
    <accessorials>
        <accessorialCd>DNC</accessorialCd>
        <accessorialCd>GUR</accessorialCd>
    </accessorials>
    <commodity>
        <grossWeight>
            <weight>500</weight>
            <weightUom>lbs</weightUom>
        </grossWeight>
        <nmfcClass>100</nmfcClass>
        <hazmatInd>false</hazmatInd>
    </commodity>
    <shipmentDate>2018-11-19</shipmentDate>
    <shipper>
        <address>
	<acctInstId>117258878</acctInstId>
            <postalCd>93727</postalCd>
        </address>
    </shipper>
    <consignee>
        <address>
            <postalCd>92027</postalCd>
        </address>
    </consignee>
</shipmentInfo>
</GetXpoLtlRateQuoteRqst>
');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

Open in new window

gr8gonzoConsultantCommented:
Ask your host to install version 1.7.6 of the extension, not the latest version.

Or you can just use curl (easier).
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Can you show me the curl code?

Thanks,

Richard
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Does 1.7.6 require php 7 or ??

Thanks
gr8gonzoConsultantCommented:
1.7.6 is an older version but it's what the PHP docs are based on. Should work actually all recent versions of PHP, including 5.x.

I'm not in front of my computer at the moment but I'll post the cURL version later.
gr8gonzoConsultantCommented:
Here's the cURL version of your request:
$strData = '<?xml version="1.0" encoding="UTF-8"?>
<GetXpoLtlRateQuoteRqst xmlns="http://www.ltl.xpo.com/API/LTLRatingAPI">
<shipmentInfo>
    ...etc...
</shipmentInfo>
</GetXpoLtlRateQuoteRqst>';

  $ch = curl_init('https://api.ltl.xpo.com/rating/1.0/ratequotes');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $strData);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/xml',
    'cache-control' => 'no-cache',
    'authorization' => 'Bearer 5ce5e6e3304fd4e069b1c00f1c1f9bab',
    'Content-Length: ' . strlen($strData)));
  $result = curl_exec($ch);
  $error = curl_error($ch);
  curl_close($ch);

echo $result;

Open in new window

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
We abandoned this approach because we could not get knowledgeable support  from XPO; we reverted to a variation of XML which we got to work.

Thanks for your efforts!

Richard
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.