Missing Class definition?

Richard Korts
Richard Korts used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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).
Commented:
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.
Richard KortsBusiness Owner / Chief Developer

Author

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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
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 Developer

Author

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

Commented:
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 Developer

Author

Commented:
Can you show me the curl code?

Thanks,

Richard
Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Does 1.7.6 require php 7 or ??

Thanks

Commented:
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.

Commented:
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 Developer

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial