Jump to content

Problem Returning Query Values


creativeplanit

Recommended Posts

Hello, I'm hoping someone will be able to help with this issue I'm having. I'm a fairly new PHP user so this is a bit outside my abilities.

Any assistance would be much appreciated.

 

The Situation:

A script hosted on Yahoo Small Business that submits a query to a third-party site and returns shipment tracking data. When I run the script on my local web server, the data is returned correctly. However when I run it from Yahoo hosting environment, no data is returned for the same query parameters.

 

The Response Header:


Server:YTS/1.19.11
P3P:policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
Date:Thu, 15 Nov 2012 13:14:05 GMT
Content-Type:text/html
Connection:close
Age:2

 

The script provided by Vendor

(Full code is attached..)

checkstatus.txt

 


<?php

// Error reporting (comment out for debug)
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

// Disable all errors and warnings (comment out in debug)
error_reporting(0);

define("COMPANY_URL", "https://xxxx.xxxxxxx.xxxxx/cargoQuery?"); // Change second argument to company URL
define("COMPANY_USERNAME", "xxxxxxxx"); // Change the second argument to company username
define("COMPANY_PASSWORD", "xxxxxxxx"); // Change the second argument to company password

// --------------------------------- DO NOT EDIT BELOW THIS LINE ---------------------------------

function get_request($url)
{
$context = stream_context_create(array(
'http' => array(
'method' => "GET",
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode(COMPANY_USERNAME . ":" . COMPANY_PASSWORD))
),
));
$response = file_get_contents($url, false, $context);
return $response;
}

$indicators = array('1' => 'Message Content Accepted',
'2' => 'Message Content Rejected with comment',
'4' => 'Goods Released',
'5' => 'Goods required for examination - referred',
'8' => 'Goods May Move under Customs transfer, Detain at Destination (CFIA)',
'9' => 'Declaration Accepted, Awaiting arrival of goods.',
'14' => 'Error message',
'23' => 'Authorised to deliver CSA Shipment',
'34' => 'Transaction awaiting processing');

$cargoNum = (trim($_GET['cargo']) != "") ? "cargo=" . strtoupper(trim($_GET['cargo'])) : "";
$transNum = (trim($_GET['transaction']) != "") ? "transaction=" .(trim($_GET['transaction'])) : "";
$data = trim(get_request(COMPANY_URL . $cargoNum . $transNum));
?>
...............
<?php
if (!empty($data))
{
echo "Data returned!";
$doc = new DOMDocument();
$doc->preserveWhiteSpace = FALSE;
$doc->loadXML($data);
$messages = $doc->getElementsByTagName('rns');
$hasAttributes = FALSE;
$duplicates = Array();
foreach ($messages as $rns)
{
$hasAttributes = TRUE;

$releaseDate = $rns->getAttribute('release_date');
if (in_array($releaseDate, $duplicates))
{
continue;
}
array_push($duplicates, $releaseDate);

$ind = $rns->getAttribute('processing_ind');
?>
<tr>
<td>
<div style="color: #2f2f2f;">
<?= $rns->getAttribute('transaction_num') ?>
</div>
</td>
<td>
<div style="color: #2f2f2f;">
<?= $rns->getAttribute('cargo_ctl_num') ?>
</div>
</td>
<td>
<div style="color: #2f2f2f;">
<?= $releaseDate ?>
</div>
</td>
<td>
<div style="color: #2f2f2f;">
<?= $rns->getAttribute('port') ?>
</div>
</td>
<td>
<div style="color: #2f2f2f;">
<?= (array_key_exists($ind, $indicators)) ? $indicators[$ind] : $ind ?>
</div>
</td>
</tr>
<?php
}
}

if (!$hasAttributes)
{
?>
<tr>
<td colspan="5">
<p><b>No data to display.</b></p>
</td>
</tr>
<?php
}
?>
......

 

The Environment

Yahoo is running PHP 5.3.6

 

My local server: PHP 5.3.1

Apache 2.2.14

Link to comment
Share on other sites

Have you tried uncommenting the error reporting lines at the beginning of the script and running it to see if there are any errors generated? Uncomment the first 2, and comment out the error_reporting(0); line.

Edited by Pikachu2000
Link to comment
Share on other sites

Thank you for your response Pikachu2000.

I modified the error reporting lines as suggested..and... error returned as

 

Warning: file_get_contents() [function.file-get-contents]: https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /status-yahoo.php on line 29

 

Warning: file_get_contents(https://xxxxxxxxxx.x...go=xxxxxxxxxxxx) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /status-yahoo.php on line 29

 

 

So, I did some searching and came up with the following:

<?php

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, ‘http://www.cnn.com’);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec($ch);

curl_close($ch);

 

// display file

echo $contents;

 

?>

 

..but I' not sure how to handle passing $context (the authorization).

Edited by creativeplanit
Link to comment
Share on other sites

Hallelujah! :happy-04: ...Working code

 

 

$curl = curl_init($url);
if($curl){
$customHeader = array(
"Authorization: Basic " . base64_encode(COMPANY_USERNAME . ":" . COMPANY_PASSWORD)
);

$curlOptArr = array(
CURLOPT_HTTPGET => TRUE,
CURLOPT_HTTPHEADER => $customHeader,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_USERAGENT => 'CuRL Request',
CURLOPT_SSL_VERIFYPEER => FALSE
);

curl_setopt_array($curl, $curlOptArr);
//$info = curl_getinfo($curl);
$response = curl_exec($curl);
$errRet = curl_error($curl);
curl_close($curl);

return $response;
} 

 

I had to include the :CURLOPT_SSL_VERIFYPEER => FALSE:" option

 

Since unable to verify SSl certificate, I was getting the following error:

 

60 - SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

 

Even though the certificate for the server I am trying to connect to is probably valid. The alternate solution as explained here requires path to certificate (not an option).

 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/CAcerts/BuiltinObjectToken-EquifaxSecureCA.crt");

 

Anyway..Thanks much

Edited by creativeplanit
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.