Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag 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
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
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
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

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

Or you can just use curl (easier).
Can you show me the curl code?

Thanks,

Richard
Does 1.7.6 require php 7 or ??

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

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