Jump to content

kicken

Gurus
  • Posts

    4,705
  • Joined

  • Last visited

  • Days Won

    179

Everything posted by kicken

  1. Since your dealing in seconds I would suggest using time() rather than microtime(). Microtime has an unusual return value by default.
  2. Your mail server is using a self-signed certificate rather than one from a trusted CA. This is why your verification is failing. Solving this requires using stream_socket_client rather than fsockopen so you can control the verification process via a stream context. There are a couple ways to address this using the context option, from best to worst: 1) Verify against a copy of your server certificate This involves saving a copy of your server's certificate along side your script in a file, then using the cafile option to tell PHP to verify the received certificate against this saved certificate. This allows the connect to complete only if the verification is successful thus preventing things such as MITM attacks. <?php $host = 'ssl://mail.example.org:993'; $context = stream_context_create(['ssl' => ['cafile' => 'mail.example.org.crt']]); var_dump(stream_socket_client($host, $error, $errorString, 30, STREAM_CLIENT_CONNECT, $context)); var_dump($errorString); var_dump($error); 2) Allow your self-signed certificate You can instruct PHP to accept a self-signed certificate using the allow_self_signed option. This will continue to verify that the host-name matches but does not verify the authenticity of the server against a known certificate. The could allow someone to intercept traffic to the server by generating another self-signed certificate with the correct host-name. 3) Disable verification entirely You can tell PHP to skip certificate verification with the verify_peer option. This will leave the connection encrypted but provides no protection against server impersonation.
  3. You do basically the same steps I outlined above for using a file, except you use redis instead. You designate a specific key to hold your API call results. Before doing your API call check if that key already has a value or not. Rather than DIY it, you could use a pre-made caching solution to make things simpler. $client = new Redis(); $client->connect('127.0.0.1', 6379); $cache = new RedisCachePool($client); $ttl = 300; $key = 'apiCache'; $results = $cache->get($key); if ($results === null){ $results = doYourApiCall(); $cache->set($key, $results, $ttl); } //Do whatever with $results
  4. Your browser is viewing the certificates you're receiving as invalid. There could be various causes for this. The browser should give you the details as to why it's rejecting the certificate somewhere, maybe behind an button you have to click. Some things to check Make sure your browser and os are up to date Check your system clock and make sure it's set to the correct date and time Might just be that the certificate actually isn't valid, so check the actual certificate. You'll have to post more details like which sites and what the exact error text is for further help.
  5. I don't think fputcsv allows you to force quotes around values. Generally speaking quotes should not be necessary except under specific circumstances. Is there any particular reason you need to have every value quoted? If you do need every value quoted, you'll have to generate the output yourself.
  6. Redis requires an external server to be running that you communicate with. If you don't already have a redis server running, the easiest thing to do would probably be to just store the cached data in a file on the server or in your database if you have one. The basic premise is the same though, you store the time along with the data when you run your API call. Before subsequent calls you check if the time since the last call has exceeded some threshold. To do this with a file you might follow these steps: Designate a particular file as your cache, store it's name in a variable. Check if that file exists. If it does not exist go to step 8 otherwise continue. Get the time it was last modified. Get the current time. Get the duration since last call by subtracting the last modified time from the current time. If the duration is less than your threshold go to step 10 otherwise continue. Perform your API call. Store the results in your cache file. Read the results of the last API call from your cache file.
  7. Simple typo, should be: echo '<pre>' . print_r($data, 1) . '</pre>';
  8. /^DT ?\d{7}$/i Details: ^ - Matches the beginning of the input string. DT - Matches the literal string DT <space>? - Matches a zero or one space character. \d{7} - Matches seven digit characters. $ - Matches the end of the input string. i - Makes the match case in-sensitive so it'll match lowercase dt as well. I would suggest using a better online tool when building and testing your expressions, such as the one linked above. It provides a breakdown of what the expression means as well as references to consult.
  9. If you're sure they are returned in oldest to newest (rather than that being just by chance) then you could simply reverse the array. If you need more control, then see the other replies for details on implementing a sort.
  10. If $needle contains a slash (/) then it will end your regex early and whatever is after it will be the flags. Presumable you're calling the function where $needle contains something like "/G". Do you actually intended for people to enter a regular expression or what? I don't really see the point of this function.
  11. That should not have been changed. Change it back to the original $row = $data[$record];
  12. @slotegraafd then perhaps you have an additional problem or did not make the correct changes. Provide the updated code after you've made the changes. I copied your function from the original post and tested it with the sample data and changes and it works just fine for me.
  13. Your data array only contains 2 rows, but your function is written to expect a minimum of ROWS_PER_PAGE rows. Once the loop passes the end of your data array, $row = $data[$record]; Will result in $row being NULL because the index it's trying to access doesn't exist. What you need to do is change your function to only loop until the end of the array if there are not enough rows available. So you want the end of your loop to be either $firstRecord + ROWS_PER_PAGE or $rows which ever is smallest. This can be easily determined by using the min() function. So change your for loop to $lastRecord = min($firstRecord + ROWS_PER_PAGE, $rows); for($record = $firstRecord; $record < $lastRecord; $record++){ Additionally, the function currently generates invalid HTML due to various errors in the HTML strings. You'll want to address those as well.
  14. What do client_select_all() and client_count() return? Move them out of the function call and assign them to a variable, then var_dump that variable. $data = client_select_all(); var_dump($data); $rows = client_count(); var_dump($rows); display_table( array( "id" => "Id", "emailaddress" => "Email", "firstname" => "First Name", "lastname" => "Last Name", "salesperson" => "Salesperson", "phonenumber" => "Phone Number", "extension" => "Extension", "type" => "Type" ), $data, $rows, 1 ); They probably are not returning what you're expecting them too.
  15. Because you're not quoting your label properly. You're going to be generating a function call that looks like myfunction(8, The world's best cider); That's invalid Javascript, your label needs to be inserted as a valid string literal so that you'd end up with a call that looks like: myfunction(8, 'The world\'s best cider'); Such a thing can be done, but it's pain to do and this whole approach is the wrong way to accomplish this anyway so you shouldn't bother trying to figure out how to quote your string. There are varying way's to do this more correctly, but the way that would closest resemble your current approach would be to create your div node and then attach a click handler to it using javascript. It appears you're using jQuery which makes this fairly easy to do. Based on your usage of record[ i ] I assume this code is inside a simple for loop that is iterating over your records array. Given that, you'd end up with something like this as a replacement: var div = $(document.createElement('div')); div.text(records[i].label); div.click(function(record){ return function(){ myfunction(record.id, record.label); }; }(records[i])); list.append(div); If you've never seen the type of function declaration being used in the .click() method, that's what's known as an Immediately Invoked Function Expression (IIFE). It basically creates a new anonymous function which is then immediately executed and it's return value (another function) is then used as the parameter to the .click() method. This whole mess is necessary to make sure you're referencing the correct record when the item is clicked. If you just used record[ i ] directly it would not work as by the time you can click on the div i would point beyond the end of your list. This is because every iteration shares the same variable scope and thus modifications made to variables inside that scope. The IIFE generates a new isolated variable scope to work around that. You can read more about this scoping issue over on Stackoverflow if you're interested, they also go over various solutions to it from the most modern to the classics. What I prefer to do is make use of either jQuery's .each() function or Array's .forEach() function to loop over my array's instead of a simple for loop. This causes every iteration of the loop to run inside it's own unique variable scope so you can easily just reference the current item. records.forEach(function(record){ var div = $(document.createElement('div')); div.text(record.label); div.click(function(){ myfunction(record.id, record.label); }); list.append(div); });
  16. If you did $rows = $stmt->fetchAll(); print_r($rows); and just see Array() on the page that would indicate your query returned zero results. Make sure your query should return results if you manually run it with the same keyword value. If you continue having trouble, show us your updated code, including your attempt to fetch and display the results. Preferable as text and not a screenshot. Use the <> button on the editor toolbar and paste your code into the dialog that opens up.
  17. Apparently you need to add the Content-length header yourself, PHP won't add it for you when passing body content. The code I posted was just to get you on the right path, you'll have to troubleshoot/problem-solve yourself to get to a final working solution. Not necessarily. Since most API's are REST based these days, most places in my experience don't really bother with an SDK. At best, you get documentation of the end points and some example code. At worst you get the "common use case" documentation and have to figure everything else out yourself. The general idea is that anyone that is going to be trying to implement said API knows how HTTP works and knows how to make HTTP requests. If you're not well-versed on HTTP then you fall back to a library that can handle that part of it for you, which for PHP a popular choice these days is Guzzle. It's still up to you to know what request to make and what data to pass however. If they have explicitly advertised their API as being AJAX compatible then maybe it is. Maybe they just forgot to add the necessary CORS stuff. Since you mention it's a private API, potentially created just for you, then this is a real possibility. If it's not advertised as being AJAX compatible though then calling it from javascript probably isn't intended and the missing CORS stuff is likely intentional.
  18. It could look like many things, depending on what you need to accomplish. The simplest thing would just be something like: <?php $url = 'https://3rdpartywebsite.com/api'; $context = stream_context_create([ 'http' => [ 'method' => 'POST' , 'content' => file_get_contents('php://input') ] ]); echo file_get_contents($url, false, $context); Basically you just have PHP make the request to the API via whatever means (native streams, cURL, guzzle, etc) and return the result back.
  19. Instead of requesting the third-party API directly in your script you request a resource on your domain then you have your server forward that request to the third-party. So, for example, your XMLHttpRequest would be to the URL http://yourdomain.com/third-party/api Then on your server you could either make that point to a PHP script that runs the third-party request and returns the result, or configure your server to proxy the request with something like: ProxyPass /third-party/api "https://3rdpartywebsite.com/api" ProxyPassReverse /third-party/api "https://3rdpartywebsite.com/api"
  20. The functions you list all say they return false on an error such as a bad path, so check for that and log the details you need to determine which path is invalid so you can fix it. $result = $xpath->evaluate($path); if ($result === false){ trigger_error('Invalid XPath: '.$path, E_USER_WARNING); } If you want to prevent the built-in warning, then use libxml_use_internal_errors. libxml_use_internal_errors(true); $result = $xpath->evaluate($path); if ($result === false){ $errors = libxml_get_errors(); $errors = array_reduce($errors, function($carry, $error){ return sprintf("%s[%s:%d] %s\r\n", $carry, $error->file, $error->line, $error->message); }, ''); trigger_error(sprintf("Invalid XPath: %s\r\nErrors: %s", $path, $errors), E_USER_WARNING); } libxml_use_internal_errors(false);
  21. $myObject->result That is a simple array. The first element in a simple array is at index zero, so just display index zero if you want the first element. var_dump($myObject->result[0]);
  22. I'm not familiar with the ESP32 in particular but usually these micro-controller products are no where near capable of running an SQL database locally. For the ones that have networking capabilities, you can usually get some sort of TCP/IP connection going either via a minimal HTTP server or custom socket setup. It looks like that's what the whole MQTT thing is so that sounds like a good option. For the ones without networking capabilities you'd generally hook them up to a computer via a serial connection and connect to it that way. This is what I've usually done for my occasional Arduino based projects.
  23. Which one? Why?
×
×
  • 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.