I know how to chain several if's in php in a row. I'm asking you because I have a mistake and I can't figure out where it comes from. I have a PHP code that allows me to query an API and retrieve a decoded PDF document depending on the user's country.
It's almost 3 times the same block but depending on the client's country the PDF changes.
- When I test the code blocks individually I have no errors.
- When I put two in a row it works fine, but if I add the third block "
elseif", although in my form I choose "United States" the code never executes and I don't understand why. If I execute this third if only the code works well.
I put the braces to delimit my 3 blocks, so I don't know where it can come from... if someone can help me to know why the 3rd elseif never executes despite the condition being true ?
<?php
if ($_POST['adresse'][5] == "France"){
$PAYS = $_POST['adresse'][5];
$PAYS = "FR";
} elseif ($_POST['adresse'][5] == "Allemagne"){
$PAYS = $_POST['adresse'][5];
$PAYS = "DE";
} elseif ($_POST['adresse'][5] == "Suisse"){
$PAYS = $_POST['adresse'][5];
$PAYS = "CH";
} elseif ($_POST['adresse'][5] == "United States"){
$PAYS = $_POST['adresse'][5];
$PAYS = "US";
}
if ( $PAYS == 'FR') //first if
{
// the soap operation which is called
$action = 'euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_createShipmentRequest';
// the xml input of the service
$xmlrequest = '<?xml version="1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
....................
</ShipmentRequest>
</soapenv:Body>
</soapenv:Envelope>';
try {
$options = array();
$options['wrapper'] = array(
'soap_version' => 'SOAP_1_1',
'encoding' => 'UTF-8',
'user_agent' => 'PHPSoapClient',
// The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.
'exceptions' => true,
// The trace option enables tracing of request so faults can be backtraced.
'trace' => true
);
$context = stream_context_create($options);
// create the soapclient and invoke __doRequest method
//WSDL URL is called on init
$client = new \SoapClient($wsdlUrl, $options);
//Here we call REQUEST URL
$output = $client->__doRequest($xmlrequest, $requestUrl, $action, 1);
} catch (SoapFault $fault) {
var_dump($fault);
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$fault->faultcode} <br/>";
echo "FaultString: {$fault->faultstring} <br/>";
echo("</p/>");
}
if (is_soap_fault($output)) {
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$output->faultcode} <br/>";
echo "FaultString: {$output->faultstring} <br/>";
} else {
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col) {
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$DHL = multiSplit($output);
$filename1 = uniqid(rand(), true) . '.txt';
$filename2 = uniqid(rand(), true) . '.pdf';
$file1 = "C:/wamp64/www/Dylan/SAV/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("C:/wamp64/www/Dylan/SAV/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
}
} elseif ( $PAYS == 'DE' || 'BE') //Second if
{
// the soap operation which is called
$action = 'euExpressRateBook_providerServices_ShipmentHandlingServices_Binder_createShipmentRequest';
// the xml input of the service
$xmlrequest = '<?xml version="1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-5">
<wsse:Username>'.$LOGIN.'</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$PASSWORD.'</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<ShipmentRequest xmlns="http://scxgxtt.phx-dc.dhl.com/euExpressRateBook/ShipmentMsgRequest">
................
</RequestedShipment>
</ShipmentRequest>
</soapenv:Body>
</soapenv:Envelope>';
try {
$options = array();
$options['wrapper'] = array(
'soap_version' => 'SOAP_1_1',
'encoding' => 'UTF-8',
'user_agent' => 'PHPSoapClient',
// The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.
'exceptions' => true,
// The trace option enables tracing of request so faults can be backtraced.
'trace' => true
);
$context = stream_context_create($options);
// create the soapclient and invoke __doRequest method
//WSDL URL is called on init
$client = new \SoapClient($wsdlUrl, $options);
//Here we call REQUEST URL
$output = $client->__doRequest($xmlrequest, $requestUrl, $action, 1);
} catch (SoapFault $fault) {
var_dump($fault);
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$fault->faultcode} <br/>";
echo "FaultString: {$fault->faultstring} <br/>";
echo("</p/>");
}
if (is_soap_fault($output)) {
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$output->faultcode} <br/>";
echo "FaultString: {$output->faultstring} <br/>";
} else {
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$DHL = multiSplit($output);
$filename1 = uniqid(rand(), true) . '.txt';
$filename2 = uniqid(rand(), true) . '.pdf';
$file1 = "C:/wamp64/www/Dylan/SAV/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("C:/wamp64/www/Dylan/SAV/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
}
} elseif ( $PAYS == 'US' || 'GB'|| 'NO'|| 'CH') //Third if
{
$xmlrequest = '<?xml version="1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</soapenv:Header>
...............................
</ShipmentRequest>
</soapenv:Body>
</soapenv:Envelope>';
try {
$options = array();
$options['wrapper'] = array(
'soap_version' => 'SOAP_1_1',
'encoding' => 'UTF-8',
'user_agent' => 'PHPSoapClient',
// The exceptions option is a boolean value defining whether soap errors throw exceptions of type SoapFault.
'exceptions' => true,
// The trace option enables tracing of request so faults can be backtraced.
'trace' => true
);
$context = stream_context_create($options);
// create the soapclient and invoke __doRequest method
//WSDL URL is called on init
$client = new \SoapClient($wsdlUrl, $options);
//Here we call REQUEST URL
$output = $client->__doRequest($xmlrequest, $requestUrl, $action, 1);
}
catch (SoapFault $fault) {
var_dump($fault);
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$fault->faultcode} <br/>";
echo "FaultString: {$fault->faultstring} <br/>";
echo("</p/>");
}
if (is_soap_fault($output)) {
echo "<h2>SOAP Fault!</h2><p>";
echo "FaultCode: {$output->faultcode} <br/>";
echo "FaultString: {$output->faultstring} <br/>";
}
else {
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
function multiSplit2($string)
{
$output = array();
$cols = explode("<DocumentImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</DocumentImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$DHL = multiSplit($output);
$DHL2 = multiSplit2($output);
$filename1 = uniqid(rand(), true) . '.txt';
$filename2 = uniqid(rand(), true) . '.pdf';
$filename3 = uniqid(rand(), true) . '.txt';
$filename4 = uniqid(rand(), true) . '.pdf';
$file1 = "C:/wamp64/www/Dylan/SAV/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$file2 = "C:/wamp64/www/Dylan/SAV/API_label/TXT/$filename3";
file_put_contents($file2, print_r($DHL2[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("C:/wamp64/www/Dylan/SAV/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
$pdf_base642 = $file2;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base642,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base642));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf2 = fopen ("C:/wamp64/www/Dylan/SAV/API_label/Commercial invoice/$filename4",'w');
fwrite ($pdf2,$pdf_decoded);
//close output file
fclose ($pdf2);
}
}
?>
Aucun commentaire:
Enregistrer un commentaire