PHP - How Do I Convert Strange Fields From Third Party Api Into Readable English
I'm getting data from a third party API and the first_name and last_name fields from this third party are sometimes not in english and they come out quite odd looking. Example...
"first_name": "Pesh\u042fo", "last_name": "\u062d\u0633\u064a\u0646" How could I normalize that using PHP to make it readable again? Similar TutorialsHi, I have a connection set up to an API using a PHP script - the API sends back data in JSON format, and if I capture it and echo it, it displays in extremely unfriendly format on the screen. I've tried to find a way to convert this data into readable format, ideally in a HTML table, but although there are articles on converting manually inputted JSON data into a HTML table using Javascript, I can't find a way to do it from a PHP variable. This is what I have so far: if ($_POST['getcompany']) { $companyname = $_POST['_Name']; $ch = curl_init(); $data_array2 = array( 'token' => $token ); $make_call2 = json_encode($data_array2); //echo 'Token is '.$token; $token2 = substr($token, 14); $token3 = substr($token2, 0, -3); //echo 'Token 2 is '.$token3; //echo 'Company Name is '.$companyname; //curl_setopt($ch, CURLOPT_GET, 1); //curl_setopt($ch, CURLOPT_POSTFIELDS, $make_call2); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: '.$token3.'')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://connectionurl?countries=GB&name='.$companyname); //Execute the request $result = curl_exec($ch); echo 'Companies: '.$result; //This displays the data provided by the search, but is in JSON format
So I need a way to get the data held in $result, into a HTML table. Any idea how I can do this? I'm trying to do this with the code below. I get an error because explode thinks I'm just sending it one argument since the string is held by an array element. If I can get the array from the explode function I can insure it into the first and last name fields in my table. Any suggestions? --Kenoli require '__classes/Db.php'; $sql = "SELECT name FROM tbl_persons"; $stmt = $pdo->query($sql); $row = $stmt->fetchall(PDO::FETCH_ASSOC); foreach ($row as $name) { $names = explode($name['name']); }
Hi all. I was wondering if there was a way (other than retrieving db fields with a query) to show something like "customer_id" as "Customer ID". Is there a functionality of this nature in either MySQL or PHP? I'm aware of preg_replace and such but that would still require two queries; one for the field names and one for the data (right?) Hi All, Sometimes after some calculation php may return this: 1.02563125481E+54 But how to make this into 1 number without the E+ etc ?! Cheers How do I write a bit of code to check if a file is NOT readable...and if (and only if) it is NOT readable (doesn't exist) , wget the file. Here's what I tried...but it loops forever and ever...even if the file does exist. Code: [Select] $filename = "file.mp3"; while(!is_readable($filename)) { $url = "domain.com" . ".mp3"; echo system('wget '.escapeshellcmd($url)); echo system('sleep 30'); } ?> I Googled...and couldn't find the correct syntax for IS NOT READABLE. Thanks! Hi Guys, I need a function which makes huge numbers normally readable... For example 1000000 must become 1.000.000 The same with smaller or lager numbers... Does someone know a simple solution ? Cheers how to tell gmail to send plan messages only i want to send my friend a pgp-crypted mail - but this fails since the html-based mails allways do some changes to the pgp my friend receives the mails in a broken style they are not readable any more plz advice me - any and all help is much appreciated by the way i use kgpg and the editor to create the crypted message. i do not like the enigmail-addon for tbird. + But if i should try this - : Which credentials are needed to configure the einigmail - the public key that i have generated recently. love to hear from you greetings Hello there, I was curious about PHP's 'time()', now is there any way to parse that into a human readable version e.g 7:53 PM, because I'm unsure on how to work with the time function since it's returned in the unix format, thanks again in advance for your help guys. Hi, I have a date and time stored in a database field (publishdate) and I'm using php to fetch it and display it in an HTML table. Only problem is it shows up in epoch time. I can't get it to display in human readable time for some reason. Here's the code I'm using (it's automatically generated by some software) // View column for publishdate field $column = new TextViewColumn('publishdate', 'Publishdate', $this->dataset); $column->SetOrderable(true); Many thanks in advance. Tony Hello, I dont really understand the for and while loops. And I have bad vocabulary so, can explain these two things in plain english. Thanks This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=331697.0 I have a comment form in which the users type thier comment in english.Those pcomments should appear in the native laguage other than english in my website.Is there any php code to do this.i dont have any idea of creating a site other than english. I found a tutorial on youtube that would allow me to create a calendar of events.
We have the calendar html page: This includes the onload="initialCalendar();" function
<script type="text/javascript"> /* <![CDATA[ */ function initialCalendar(){ var hr = new XMLHttpRequest(); var url = "calendar/calendar_start.php"; var currentTime = new Date (); var month = currentTime.getMonth() + 1; var year = currentTime.getFullYear(); showmonth = month; showyear = year; var vars= "showmonth="+showmonth+"&showyear="+showyear; hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if (hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("showCalendar").innerHTML = return_data; } } hr.send(vars); document.getElementById("showCalendar"). innerHTML = "processing..."; } /* ]]> */ </script> <script type="text/javascript"> /* <![CDATA[ */ function next_month() { var nextmonth = showmonth + 1; if(nextmonth > 12) { nextmonth = 1; showyear = showyear+1; } showmonth = nextmonth; var hr = new XMLHttpRequest(); var url = "calendar/calendar_start.php"; var vars= "showmonth="+showmonth+"&showyear="+showyear; hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if (hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("showCalendar").innerHTML = return_data; } } hr.send(vars); document.getElementById("showCalendar"). innerHTML = "processing..."; } /* ]]> */ </script> <script type="text/javascript"> /* <![CDATA[ */ function last_month() { var lastmonth = showmonth - 1; if(lastmonth < 1 ) { lastmonth = 12; showyear = showyear-1; } showmonth = lastmonth; var hr = new XMLHttpRequest(); var url = "calendar/calendar_start.php"; var vars= "showmonth="+showmonth+"&showyear="+showyear; hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if (hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("showCalendar").innerHTML = return_data; } } hr.send(vars); document.getElementById("showCalendar"). innerHTML = "processing..."; } /* ]]> */ </script> <script type="text/javascript"> /* <![CDATA[ */ function overlay() { el = document.getElementById("overlay"); el.style.display = (el.style.display == "block") ? "none" : "block"; el = document.getElementById("events"); el.style.display = (el.style.display == "block") ? "none" : "block"; el = document.getElementById("eventsBody"); el.style.display = (el.style.display == "block") ? "none" : "block"; } /* ]]> */ </script> <script type="text/javascript"> /* <![CDATA[ */ function show_details(theId) { var deets = (theId.id); el = document.getElementById("overlay"); el.style.display = (el.style.display == "block") ? "none" : "block"; el = document.getElementById("events"); el.style.display = (el.style.display == "block") ? "none" : "block"; var hr = new XMLHttpRequest(); var url = "calendar/events_fns.php"; var vars = "deets="+deets; hr.open("POST", url, true); hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange= function() { if (hr.readyState == 4 && hr.status == 200) { var return_data = hr.responseText; document.getElementById("events").innerHTML = return_data; } } hr.send(vars); document.get ElementById("events").innerHTML = "processing..."; } /* ]]> */ </script>The script to show to actual calendar is called from here.... calendar_start.php <?php $showmonth = $_POST['showmonth']; $showyear = $_POST['showyear']; $showmonth= preg_replace('#[^0-9]#i', '', $showmonth); $showyear= preg_replace('#[^0-9]#i', '', $showyear); $day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear); $pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear)); $post_days = (6-(date('w', mktime(0,0,0, $showmonth, $day_count, $showyear)))); echo '<div id="calendar-wrap">'; echo '<div class="title-bar">'; echo '<div class="previous-month"><input name="button" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>'; echo '<div class="show-month">' . date('F', mktime(0, 0, 0, $showmonth)) . ' ' . $showyear . '</div>'; echo '<div class="next-month"><input name="button" type="submit" value="Next Month" onClick="javascript:next_month();"></div>'; echo '</div>'; echo '<div class="week_days">'; echo '<div class="days-of-week">Sun</div>'; echo '<div class="days-of-week">Mon</div>'; echo '<div class="days-of-week">Tues</div>'; echo '<div class="days-of-week">Wed</div>'; echo '<div class="days-of-week">Thur</div>'; echo '<div class="days-of-week">Fri</div>'; echo '<div class="days-of-week">Sat</div>'; echo '<div class="clear"></div>'; echo '</div>'; //Previous Month days if ($pre_days != 0) { for($i=1; $i<=$pre_days; $i++) { echo '<div class="non-cal-days"></div>'; } } //Current Month Days $conn = mysqli_connect('Databaseconnection Things') or die ("Could not connect to the Database"); for ($i=1; $i<= $day_count; $i++) { //get event logic $date = $i.'/'.$showmonth.'/'.$showyear; $query = mysqli_query('SELECT calid FROM calendar WHERE caldate = "'.$date.'"') or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($query), E_USER_ERROR); $num_rows = mysqli_num_rows($conn, $query); if($num_rows > 0) { $event = "<input name='$date' type='submit' value='Details' id='$date' onClick='javascript:show_details(this);'>"; } echo '<div class="cal-days">'; echo '<div class="day-heading">' . $i . '</div>'; if($num_rows != 0) { echo "<div class='opening'><br/>" . $event . "</div>";} echo '</div>'; } //Next Months Days if ($post_days !=0) { for($i=1; $i<=$post_days; $i++) { echo '<div class="non-cal-days"></div>'; } } echo '</div>'; ?>And events_fns. <?php $deets = $_POST['deets']; $deets = preg_replace('#[^0-9/]#i', '', $deets); $conn = mysqli_connect("Database Connection") or die ("Could not connect to the Database"); $events = ''; $query = mysqli_query('SELECT calid FROM calendar WHERE caldate = "'.$deets.'"') or die ("Error:".mysqli_errno()); //echo "$query"; $num_rows=0; if ($result = mysqli_query($query,$conn)) { $num_rows = mysqli_num_rows($result); } if ($num_rows > 0) { $events .= '<div id="eventsControl"><button onMouseDown="overlay()">Close</button><br /><br />'.$deets.'<br /><br /></div>'; while ($row = mysqli_fetch_array($query)) { $title = $row['eventtitle']; $desc = $row['description']; $loc = $row['eventlocation']; $events .='<div id="eventsBody">'.$title.'<br />'.$desc.'<br />'.$loc.'<hr /></div>'; } } echo $events; ?>I have posted all the code so you can see how it fits together ect ect however the issue I am having is an error message that repeats across the page. it reads: Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home/sites/agile-cms.co.uk/public_html/mfcf/calendar/calendar_start.php on line 49 Fatal error: Query Failed! SQL: - Error: in /home/sites/agile-cms.co.uk/public_html/mfcf/calendar/calendar_start.php on line 49 for ($i=1; $i<= $day_count; $i++) { //get event logic $date = $i.'/'.$showmonth.'/'.$showyear; $query = mysqli_query($conn, 'SELECT eventid FROM events WHERE eventdate = "'.$date.'"') or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($query), E_USER_ERROR); $num_rows = mysqli_num_rows($query); if($num_rows > 0) { $event = "<input name='$date' type='submit' value='Details' id='$date' onClick='javascript:show_details(this);'>"; } echo '<div class="cal-days">'; echo '<div class="day-heading">' . $i . '</div>'; if($num_rows != 0) { echo "<div class='opening'><br/>" . $event . "</div>";} echo '</div>'; }I think I have missed something being so close to it and I think I need an outside P.O.V to look at it and point me in the right direction.... Any help would be much appreciated.... This topic has been moved to PHPFreaks.com Questions, Comments, & Suggestions. http://www.phpfreaks.com/forums/index.php?topic=343248.0 I have a site with a shopping cart that uses a paypal buy now button. The button seems to work fine, when I click it everything in my cart on my site shows up how its suppose to in the paypal checkout page. Then problem is when I try to log into my buyer sandbox account. It's telling me its the wrong email or password when I know for a fact it is correct. I know I need to be logged in to the sandbox developer account while using it, but it still wont let me log in. My site tried to do multiple items using the value _cart for the cmd hidden paypal input and upload. However when I use another example that only does one item at a time and has preset variables my sandbox paypal accoung logs in fine. This makes me think there is something wrong with my code. Here is the code to build my paypal button for my site Code: [Select] $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" accept-charset="utf-8"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value="pwang@sofpower.com">'; // Start the For Each loop $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $query = "SELECT * FROM product WHERE id='$item_id' LIMIT 1"; $result = $db_obj->query($query); if($db_obj->error) exit ($db_obj->error); //$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; $_SESSION["cart"] = $cartTotal; setlocale(LC_MONETARY, "en_US"); //$pricetotal = money_format("%10.2n", $pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_'. $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>$' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } setlocale(LC_MONETARY, "en_US"); //$cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= ' <input type="hidden" name="return" value="http://webdev.cs.kent.edu/~jfunchio/wp2/HW2/checkout_complete.php"> <input type="hidden" name="cancel_return" value="http://webdev.cs.kent.edu/~jfunchio/wp2/HW2/paypal_cancel.php"> <input type="image" src="https://www.paypal.com/images/x-click-butcc.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!"> </form>'; } Here is the example that works Code: [Select] <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta charset="utf-8"/> <title>SuperStore.Com</title></head> <body style="margin: 50px"> <h2>Purchase Our Product Online</h2> <p><img src="hammer.jpeg" alt="Hammer" style="vertical-align: middle" /> Hammer for $4.49.</p> <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" accept-charset="utf-8"> <input type="hidden" name="cmd" value="_xclick" /> <input type="hidden" name="business" value="jfunchio@kent.edu" /> <input type="hidden" name="item_name" value="Hammer" /> <input type="hidden" name="amount" value="4.49" /> <input id="tt" type="hidden" name="return" value="success return URL" /> <input id="ff" type="hidden" name="cancel_return" value="failure return URL" /> <!-- company logo for PayPal page --> <input type="hidden" name="image_url" value="http://www.sofpower.com/images/sofppal.gif" /> <input type="hidden" name="cn" value="Special Instructions (optional)" /> <table style="background-color: #ddffee"> <tr><td> </td></tr> <tr><td>Customer Full Name</td><td> <input type="text" name="cnm" size="30" maxlength="50" /></td></tr> <tr><td><input type="hidden" name="on0" value="customer email address" /> Customer email</td> <td><input type="text" name="os0" size="30" maxlength="100" /></td></tr> <tr><td><input type="hidden" name="on1" value="customer institution" /> Customer institution</td> <td><input type="text" name="os1" size="30" maxlength="100" /></td></tr> <tr><td> </td></tr> </table><br /> <input type="image" src="https://www.paypal.com/images/x-click-butcc.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" /> </form> <p>When payment has been received, we will also send a confirmation to you by email.<br /> After ordering, if you need to get in touch with us, please send email to<br /> <tt>customer-service@superstore.com</tt>.</p> <pre> </pre> </body></html> I have had a customer want to run my application (PHP/APACHE/MYSQL) on their server rather than a commercial hosting offering (JUSTHOST/GODADDY)
I am reluctant, as it means giving them access to my PHP code which could possibly be copied or distributed.
Can I protect against this?
Happy New Year, folks! I am having an issue that's been dragging my life for quite some time. I am creating a website for my church maranatha.tv The site's Menu and Content are pulled from a MySQL database I created. As far as this goes, everything is fine; content is pulled from my database with no issues. My problem is as follow: I am including an online bible, which is a third party script I downloaded. This scripts comes with its own database, which I have installed for use in my web server. I used Include() to include the index.php file of the online bible script, from its folder. I just don't know if this the right way to do it. Of course, this script has its own folder and a set of files which makes up the entire bible script. I use an if condition so that when the user clicks on the menu button BIBLE, the script's index.php file is included instead of text from my database. This way of adding the third party script is rendering some unwanted results such as layout distortion (which I don't care at this point), broken links (main issue), and links (although broken) are sent to new pages, instead of staying within my site's CONTENT page template. I need to find a way to make my script more modular so everything renders as intended. Here's my content function: Code: [Select] function content(){ // DETERMINE which page ID to USE in our query below ******************** if (!isset($_GET['jesusid'])) { $pageid = '1'; } else { $pageid = preg_replace('#[^0-9]#i', '', $_GET['jesusid']);} // filter everything but numbers for security) //preg_replace($pattern, $replacement, $string);//preg_replace() Function structure // Query the body section for the proper page $query = mysql_query ("SELECT body_text,title,linklabel, author FROM content WHERE id = '$pageid' LIMIT 1 ") or die (mysql_error()); while ($row = mysql_fetch_array($query)) { echo ucwords($row['title']).' por '; echo '<b>'.$row['author']. '</b><br>'; echo ucwords($row['body_text']); //Add Bible Script if (ucwords($row['title'])=='Biblia') //use row title -- UPPERCASED word { include ('bible/__WINDOWS/search.php'); } } } ?> Just click on the BIBLE button, and then on any link within that page and you will see what I mean. I am still learning PHP and I don't have any background integrating third party scripts to an existing PHP website. I hope someone can help me. Thanks in advance for your assistance. Hi guys, what I'm struggling to do is 1) Users land on https://www.mysite.com/login.php 2) Users type their email and password 3) POST data submitted to http://www.3rdparty.com/login.php with cURL 4) Users redirected to http://www.3rdparty.com/index.php (logged in). I've been using this simple form to POST directly to the 3rd party site. Code: [Select] <form name="loginform" method="post" target="_blank" action="http://www.3rdparty.com/login.php"> Email <input name="email" type="text"> Password<input name="password" type="password"> <input name="submit" type="submit" id="loginbutton" value="login"></form> This works great. But now I've installed a SSL on my site and I've just realised that using the form above, the data is still POSTed as a plain text because the 3rd party site is not https. So I want to submit the form to my login.php form and let this form take the users to the 3rd party site. So at least the user inputs to my site is encrypted. My new code looks like this. Code: [Select] <form name="loginform" method="post" target="_blank" action="login.php"> Email <input name="email" type="text"> Password<input name="password" type="password"> <input name="submit" type="submit" id="loginbutton" value="login"></form> <?php if(isset($_POST['email'])) $email= $_POST['email']; if(isset($_POST['password'])) $password= $_POST['password']; if(isset($_POST['submit'])) $submit = $_POST['submit']; $Curl_Session = curl_init('http://www.3rdparty.com/login.php'); curl_setopt ($Curl_Session, CURLOPT_POST, 1); curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "email=$email&password=$password&submit=$submit"); curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec ($Curl_Session); curl_exec ($Curl_Session); curl_close ($Curl_Session); print $result; ?> What this code is doing now is it's just rendering the www.3rdparty.com's login page (not logged in) on my site. When I type wrong values, it renders www.3rdparty.com's login page with an error message on it. So I think at least the values are being POSTed but it doesn't log me in. All of the cURL codes available out there seem to POST the data and fetch some results back not redirecting the users to another site. My ultimate goal is to POST the form and redirect the users to the 3rd party site's member area as well. I tried header("Location: http://www.3rdparty.com/index.php"); but it just takes user to that page without being logged in. Could anyone give me some hints? This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=358932.0 Hey guys,
Thank you in advance... here is my situation, I have a form with three (3) fields in it, the 'student name' is unlimited textfield with an "add more" button to it and I have two select fields ('number of shirts' and 'trophies') that depend on the number of entries for 'student name'...
I want to create the select fields based on this math, for as many 'student name' entries:
1- i want to have the select form for 'number of shirts' to be 0 up to that number... so if there are 6 'student name' entries, the select options will be 0,1,3,4,5,6,7
2- I want to have the select form for 'trophies' to be 5 'student name' entries to 1 'trophies', for example if there are 6 'student name' entries, the select options will be 0,1... if there are 13 entries, options will be 0,1,2... So if there are less than 5 'student name' entries, the select field will not show (hidden)
of course if there are no 'student name' entries, these two fields won't show up (hidden)
let me know if that make sense and ANY help or directions will be GREATLY APPRECIATED.
Thanks guys!
|