Richard Korts
asked on
Missing Class definition?
I have this code, provided by someone else:
It fails with: Fatal error: Class 'HttpRequest' not found in /home/lakoshva/public_html /freight_t est.php on line 3
Doesn't there have to be something like "require once xxxxxxxxxxxxxclass.php"?
Thanks
<?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;
}
It fails with: Fatal error: Class 'HttpRequest' not found in /home/lakoshva/public_html
Doesn't there have to be something like "require once xxxxxxxxxxxxxclass.php"?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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_t est.php on line 3 and defined in /home/lakoshva/public_html /HttpReque st.php on line 5
Fatal error: Call to a member function setUrl() on null in /home/lakoshva/public_html /HttpReque st.php on line 11
The current code (in case I made changes) is
Warning: Missing argument 1 for HttpRequest::__construct()
Fatal error: Call to a member function setUrl() on null in /home/lakoshva/public_html
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;
}
Ask your host to install version 1.7.6 of the extension, not the latest version.
Or you can just use curl (easier).
Or you can just use curl (easier).
ASKER
Can you show me the curl code?
Thanks,
Richard
Thanks,
Richard
ASKER
Does 1.7.6 require php 7 or ??
Thanks
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.
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;
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
Thanks for your efforts!
Richard
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).