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

Tideways Blog:
Using HTTP client timeouts in PHP
Jan 06, 2017 @ 17:57:10

The Tideways blog has a post sharing things you can do in PHP to work with HTTP client timeouts in things that use the PHP sockets and streams.

Timeouts are a rarely discussed topic and neglected in many applications, even though they can have a huge effect on your site during times of high load and when dependent services are slow. For microservice architectures timeouts are important to avoid cascading failures when a service is down.

The default socket timeout in PHP is 60 seconds. HTTP requests performed with for example file_get_contents, fopen, SOAPClient or DOMDocument::load are using this timeout INI setting to decide how long to wait for a response.

He talks some about how these timeouts can effect your script and some of the common reactions (in code) to them happening. He then shows how to configure these timeouts to match the needs of you application in a few ways:

  • globally in the ini configuration
  • on a per-call basis in a stream_context_create call
  • changing the load timeout for DOMDocument::load
  • updating the setting for calls with SOAPClient
  • changing the timeout on cURL extension calls

Each item on the list comes with the code/settings needed to make the change.

tagged: timeout socket stream domdocument soapclient curl tutorial

Link: https://tideways.io/profiler/blog/using-http-client-timeouts-in-php

James Morris' Blog:
Parsing HTML with DOMDocument and DOMXPath::Query
Jun 27, 2012 @ 15:19:35

In the latest post to his blog James Morris looks at using XPath's query() function to locate pieces of data in your XML.

The other day I needed to do some html scraping to trim out some repeated data stuck inside nested divs and produce a simplified array of said data. My first port of call was SimpleXML which I have used many times. However this time, the son of a bitch just wouldn’t work with me and kept on throwing up parsing errors. I lost my patience with it and decided to give DomDocument and DOMXpath a go which I’d heard of but never used.

He includes a code (and XML document) example showing how to extract out some content from an HTML structure - grabbing each of the images from inside a div and associating them with their description content.

tagged: dom domdocument domxpath xpath tutorial html

Link:


Trending Topics: