Avatar of Richard Korts
Richard Korts
Flag for United States of America asked on

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
PHP

Avatar of undefined
Last Comment
Richard Korts

8/22/2022 - Mon
gr8gonzo

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).
ASKER CERTIFIED SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Richard Korts

ASKER
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
gr8gonzo

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Richard Korts

ASKER
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

gr8gonzo

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

Or you can just use curl (easier).
Richard Korts

ASKER
Can you show me the curl code?

Thanks,

Richard
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Richard Korts

ASKER
Does 1.7.6 require php 7 or ??

Thanks
gr8gonzo

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.
gr8gonzo

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Richard Korts

ASKER
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