Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

TutsPlus.com:
Parsing HTML With PHP Using DiDOM
Jul 05, 2018 @ 15:48:35

The TutsPlus.com site has posted a tutorial showing you how to use the DiDOM library to parse HTML in PHP. The DiDOM is a "simple and fast parser" packed with a lot of functionality for parsing, searching and modifying HTML.

Every now and then, developers need to scrape webpages to get some information from a website. For example, let's say you are working on a personal project where you have to get geographical information about the capitals of different countries from Wikipedia. Entering this manually would take a lot of time. However, you can do it very quickly by scraping the Wikipedia page with the help of PHP. You will also be able to automatically parse the HTML to get specific information instead of going through the whole markup manually.

In this tutorial, we will learn about a fast, easy-to-use HTML parser called DiDOM. We will begin with the installation process and then learn how to extract information from different elements on a webpage using different kinds of selectors like tags, classes, etc.

The tutorial starts by helping you get the package installed (via Composer) and provides a simple example of using it to parse either a string of HTML, a local document or a remote site. It then walks you through using the search functionality built into the library, using either CSS selector type strings. They also include examples of traversing the DOM, updating element attributes, and adding/removing/replacing elements.

tagged: tutorial dom parse html didom package elements

Link: https://code.tutsplus.com/tutorials/parsing-html-with-php-using-didom--cms-31242

Laravel News:
PHP 7.3: A Look at JSON Error Handling
Jun 13, 2018 @ 15:18:53

On the Laravel News site there's a tutorial posted looking ahead at PHP 7.3 and some of the changes coming for JSON error handling.

One of the new features coming to PHP 7.3 is better error handling for json_encode() and json_decode(). The RFC was unanimously accepted by a 23 to 0 vote. Let’s take a look at how we handle JSON errors in <= PHP 7.2, and the new improvements coming in PHP 7.3.

They start with an example of how PHP developers would normally check for JSON parsing errors and the typical response when it fails. In the proposed functionality for PHP 7.3 and optional JSON_THROW_ON_ERROR would be added to throw a JsonException if there was an issue parsing the provided data. This also means that you no longer need to manually request the error message, it would just come through as a part of the standard exception. You can find out the full details on the change in the RFC.

tagged: php73 json parse error handling throwable exception feature rfc

Link: https://laravel-news.com/php-7-3-json-error-handling

Zend Framework Blog:
Discover and Read RSS and Atom Feeds
Apr 07, 2017 @ 14:25:08

On the Zend Framework blog Matthew Weier O'Phinney has written up a new tutorial showing you how to discover and read RSS feeds with the help of the zend-feed component of the Zend Framework.

Remember RSS and Atom feeds? Chances are, you may be reading this because it was on a feed.

[...] An interesting fact: Atom itself is often used as a data transfer format for REST services, particularly content management platforms! As such, being familiar with feeds and having tools to work with them is an important skill for a web developer! In this first of a two part series on feeds, we'll look at feed discovery, as well as reading, using zend-feed's Reader subcomponent.

He gets started by installing the zendframework/zend-feed component with Composer and pulling in the zendframework/zend-http component to make the HTTP requests for the feeds. He then shares some code that helps with RSS/Atom feed discovery on a site and viewing the results. This list is then used as sources to import and code is shown that outputs the basic information about the feed. Finally he shows how to look through the entries in the feed and output the title, link and description of each.

tagged: series discover read parse rss atom feed zendframework zendfeed zendhttp tutorial part1

Link: https://framework.zend.com/blog/2017-04-06-zend-feed-reading.html

SitePoint PHP Blog:
Diffbot: Crawling with Visual Machine Learning
Aug 01, 2014 @ 16:37:12

On the SitePoint PHP blog Bruno Skvorc has posted a tutorial showing you how to use the Diffbot service to extract data from any page. He introduces both the service itself and walks you through a simple request via Guzzle.

Have you ever wondered how social networks do URL previews so well when you share links? How do they know which images to grab, whom to cite as an author, or which tags to attach to the preview? Is it all crawling with complex regexes over source code? Actually, more often than not, it isn’t. [...] If you want to build a URL preview snippet or a news aggregator, there are many automatic crawlers available online, both proprietary and open source, but you seldom find something as niche as visual machine learning. This is exactly what Diffbot is – a “visual learning robot” which renders a URL you request in full and then visually extracts data, helping itself with some metadata from the page source as needed.

He uses a combination of a Laravel installation (via a Homestead instance) and a Guzzle request using a fetched token. The service offers a 10k call limit on a 7 day free trial, so you can sign up and grab your token there. He includes code for an example request fetching a SitePoint page and parsing out the tags. He also briefly looks at the custom handling diffbot allows based on CSS-type rules.

tagged: diffbot parse data service api guzzle homestead tutorial introduction

Link: http://www.sitepoint.com/diffbot-crawling-visual-machine-learning/

Joshua Thijssen:
Internal PHP function usage
Jul 28, 2014 @ 15:05:39

Curious about the usage of the various "internal" (built-in, not user defined) functions in use is a wide range of PHP applications, Joshua Thijssen did some research on GitHub and has shared the results on his site today.

How many internal PHP functions (things like count(), strpos(), array_merge() etc), does PHP have? Depending on which version you use, and how many extensions you have loaded, somewhere between 1000 and 2000 would be a good guess. But how many of these internal functions are you REALLY using?

He created a custom script to fetch the results of a custom query (one that found repos with over fifty stars), grabbed the source and parsed the results looking for these internal functions. He shares the results of his parsing from 967 repos in the remainder of the post, including: the top ten most called, some interesting facts found in the results and some of the "bad" ones in wide use (like "exec" and "mysql_connect").

tagged: internal function usage statistics github parse query

Link: https://www.adayinthelifeof.nl/2014/07/25/internal-php-function-usage/

SitePoint PHP Blog:
Piping Emails to a Laravel Application
Feb 17, 2014 @ 15:13:48

On the SitePoint PHP blog there's a new tutorial posted about piping emails to Laravel (well, a Laravel-based application). He shows how to have your application take data in from the current input, parse it and insert the data into a database.

In project management or support management tools, you will see this a lot: you can reply to an email message and it is automatically visible in a web application. Somehow, these tools were able to get those email messages right into their system. In this article, we are going to have a look at how we can pipe emails to our Laravel 4 application.

He walks you through the creation of an Artisan command, "email.parse", and using the PHP MIME Mail Parser library to extract data. He gets the to, from, title and message contents from the email and shows how to work with attachments too. Finally, he shows how to set up the mail server to pipe the incoming email though the PHP script for parsing.

tagged: email parse message laravel tutorial mail server

Link: http://www.sitepoint.com/piping-emails-laravel-application/

Evert Pot:
jCard is now a thing
Jan 21, 2014 @ 17:04:18

In his most recent post Evert Pot talks about jCard, a JSON-based format that was recently approved to serve up VCard personal information data in an easier-to-parse format.

I'm a big fan of this format. vCards have been around since 1995, and even though we've had a pretty significant update in 2011 in the form of vCard 4.0, the format is still complicated to parse, has a number of problems that go all the way back to the early days. [...] The biggest problem with vCards, is that upon a first glance, the format seems extremely easy to parse and generate with just a couple of string manipulation functions. When you dig deeper into the specifications though, you'll notice that it's actually really complex and hosts a ton of edge cases.

He includes an example of how to generate the jCard format using the Sabre/Object and the resulting output, both in the traditional vCard format and the new jCard structure.

tagged: vcard jcard json format sabre object parse output

Link: http://evertpot.com/jcard-completed

Igor Wiedler:
Evolving syntax
Jul 31, 2013 @ 16:44:07

In a new post to his site Igor Wiedler looks forward and suggests some alternate syntax for PHP based around the idea of macros from Lisp. These macros would be parsed at runtime and handled directly as code, compiled down from their custom format.

A very common problem that many software projects have is lack of adoption of new versions. Browsers are an excellent example of this, But it exists on the server as well. [...] This leads to this recursive problem of hosting companies not upgrading because they don't have to, and software not requiring newer versions of their programming language, because they don't want to lose their users. The longer your dependency chain is, the more you suffer from this.

He points out that the easier it is to update these lower level pieces, the simpler it is to introduce new things into your system. He suggest that macro-like functionality for PHP could aid in this goal. He talks some about backporting features and how these marcos could make it easier to upgrade just the things we wanted (or all of them) without having to upgrade PHP itself. He even went so far as to create a tool (galapagos) that does this kind of parsing. His examples implement the 5.4 features of short arrays, $this in closures, function array dereferencing and callable typehinting.

Being able to invent your own syntax is very useful, which instantly becomes apparent when you look at the past. Features get added to languages all the time. What if you could do that easily, within minutes instead of months?
tagged: evolve syntax lisp macro feature galapagos parse ast language

Link: https://igor.io/2013/07/26/evolving-syntax.html

PHPBuilder.com:
Building a PHP RSS Aggregator
Apr 04, 2013 @ 18:09:13

On PHPBuilder.com today there's a quick tutorial showing you how to build an RSS aggregator that can pull in RSS content and drop it into a MySQL table.

RSS stands for Really Simple Syndication. It is a Web format that allows website owners to distribute their latest and frequently updated content in a standardized way. RSS feed is actually an XML document that can be easily read by using RSS reader software or built-in functions in programming languages, such as PHP or Java. In this article, the focus will be on building a RSS aggregator in PHP.

They introduce the basics of an RSS feed - a specially formatted XML document with values for individual posts (like "title" and "link". They provide the SQL structure for the "article" and "feed" tables and the code to pull out each "feed" record, parse it and drop that into the "article" table for later consumption. They show two different methods for getting the content - one using file_get_contents and another using cURL.

tagged: rss aggregator tutorial mysql database parse

Link: http://www.phpbuilder.com/articles/php-functions/xml/building-a-php-rss-aggregator.html

PHPMaster.com:
Parsing XML With SimpleXML
Feb 12, 2013 @ 18:48:34

On PHPMaster.com today there's a new tutorial introducing you to SimpleXML, a handy bit of functionality included with the base PHP install to make working with XML (well, reading it) much simpler.

Parsing XML essentially means navigating through an XML document and returning the relevant data. An increasing number of web services return data in JSON format, but a large number still return XML, so you need to master parsing XML if you really want to consume the full breadth of APIs available. Using PHP’s SimpleXML extension that was introduced back in PHP 5.0, working with XML is very easy to do. In this article I’ll show you how.

He starts with some basic usage of the SimpleXML parsing, giving an example XML to parse, the resulting object and how to access the data inside it. There's also a bit about dealing with namespaces in the XML you're parsing and a more practical example - parsing the output of a YouTube feed to get links to various videos.

tagged: parse xml simplexml introduction tutorial

Link:


Trending Topics: