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

Delicious Brains Blog:
PHP and cURL: How WordPress makes HTTP requests
Mar 30, 2017 @ 15:49:35

In a new post from the Delicious Brains site Peter Tasker looks at how WordPress makes HTTP requests with the help of the cURL functionality in PHP.

cURL is the workhorse of the modern internet. As its tagline says, cURL is a utility piece of software used to ‘transfer data with urls‘. According to the cURL website, the library is used by billions of people daily in everything from cars and television sets, to mobile phones. It’s the networking backbone of thousands of applications and services. Unsurprisingly, it’s also a core utility used by WordPress’ own Requests API as well as our own WP Migrate DB Pro.

If you’re curious about the power of the cURL library, how it works with WordPress and what to watch out for (especially on macOS), then you’re in the right place.

He starts by giving a bit of background on what cURL is and some examples of how its used to make requests. He then talks about the cURL integration with PHP via an extension and provides a simple code example fetching an endpoint from the httpbin.org site. With that background defined he moves into the main focus of the article - how cURL and PHP combine in the WordPress WP_Http class and Requests handling to make HTTP requests to remote (or local) resources. Code examples are included showing how to put these pieces to work in a custom script and includes some common issues you might see during your HTTP request development.

tagged: wordpress http request curl tutorial wphttp internal example

Link: https://deliciousbrains.com/php-curl-how-wordpress-makes-http-requests/

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

MyBuilder Tech Blog:
Managing Jenkins Project Builds and Configuration using PHP
Jul 08, 2015 @ 14:57:22

The MyBuilder.com Tech blog has posted a tutorial showing you how to manage your Jenkins builds and configuration using PHP via the Jenkins REST API interface and a few handy curl calls.

I decided to create a simple PHP console script that can be run (or added as a git-hook) to maintain synchronization between the branch you are working on and the branch Jenkins is building. Whilst developing this script, it dawned on me that many other automated use-cases could be achieved with the ability to easily update a projects configuration file. [...] Jenkins fortunately provides us with a RESTful interface to manage typical tasks and activities. Included in this is the ability to read the XML configuration file for a specified project.

The tutorial includes examples of requests you can make to the API to do things like:

  • Reading the Projects Configuration
  • Writing to the Projects Configuration
  • Sending a Project Build Request

The post ends with the full script, a procedural process that executes each of the above steps in order (with an interesting use of the "goto" functionality).

tagged: manage jenkins build project configuration rest api tutorial curl

Link: http://tech.mybuilder.com/managing-jenkins-project-builds-and-configuration-using-php/

Hack Blog:
Async – Cooperative Multitasking for Hack
Dec 08, 2014 @ 17:56:54

On the Hack blog there's a new post talking about async, a feature in Hack that allows for code to "cooperatively multitask". This gives the language a way to keep moving on in the execution without having to wait for things like database queries or remote file fetches to finish.

This is somewhat similar to threading, in that multiple code paths are executed in parallel, however it avoids the lock contention issues common to multithreaded code by only actually executing one section at any given moment. “What’s the use of that?”, I hear you ask. You’re still bound to one CPU, so it should take the same amount of time to execute your code, right? Well, that’s technically true, but script code execution isn’t the only thing causing latency in your application. The biggest piece of it probably comes from waiting for backend databases to respond to queries.

She gives the example of pulling in a remote file (HTTPS, where there's a bit more latency) and how to use async, await, WaitHandle, and Awaitable to work around the timing issue. She shows how to make a method asynchronous and how to join the results of the operation back up with the rest of the script. This includes the use of various "handles" including RescheduleWaitHandle, SleepWaitHandle and the AwaitAllWaitHandle. She shows the integration of a custom cURL handler that makes use of this processing, marked async, to multithread the requests to the remote server(s).

tagged: hack async asynchronous multitasking curl example remote fetch language

Link: http://hhvm.com/blog/7091/async-cooperative-multitasking-for-hack

SitePoint PHP Blog:
Installing PHP Extensions on Nitrous.io
Mar 03, 2014 @ 17:45:22

On the SitePoint PHP blog Bruno Skvorc has posted a new tutorial showing you how to get PHP extensions installed on Nitrous.io, an online environment combining an IDE and PaaS hosting.

Inspired by a comment on my previous article, I realized Nitrous was still a bit too complicated to customize properly. In this tutorial, we’ll glide through installing cURL and Phalcon on a Nitrous.io PHP box.

He continues on from his previous article and shows how to detect cURL support and how to build it from the PHP source into an extension. He helps you get the source for the older PHP version Nitrous.io has installed and the commands you'll need to build the extension. With it installed and enabled in the php.ini, he also installs the Phalcon extension.

tagged: nitrousio tutorial install extension curl phalcon compile

Link: http://www.sitepoint.com/installing-php-extensions-nitrous-io

SitePoint PHP Blog:
Using Google Translate API with PHP
Oct 31, 2013 @ 18:14:11

On the SitePoint PHP blog Jacek Barecki has a new tutorial showing you how to use the Google Translate API to handle the translation of dynamic input that may have come from other sources (including users).

If your site serves visitors from different countries, you may already have translated all its static content into several languages. But what to do with the content posted daily by the users in comments, opinions and ratings? As this may be as valuable a part of your site as the static content, you should think of finding a way to translate it into other languages. One service that can help is, of course, Google Translate.

He walks you through the process of setting up a Google API account (with screenshots) and how to turn on the Translate API specifically. The Translate API is not a free service, unfortunately, so you'll need to set up some billing information to use it. He then points you to where you can find your API key and shows a sample API call to get the currently supported languages. With that working, he shows you how to make an actual translation call, passing in the text and desired language on the URL and sending it to the API via curl. He also talks some about handling errors based on HTTP response code and the message returned.

tagged: google translate api tutorial curl

Link: http://www.sitepoint.com/using-google-translate-api-php/

Phil Sturgeon:
CurlFile and the Facebook SDK in PHP 5.5
Aug 30, 2013 @ 16:19:04

Phil Sturgeon has a new post to his site today looking at a new feature that's included with PHP 5.5, Curlfile, and how he uses it with calls to the Facebook API (and a fix to make it cooperate).

One of the features implemented in PHP 5.5 was CurlFile, a nice addition to the Curl extension to allow you to specify specific arguments as a file for upload. In previous versions (pre-PHP 5.5) the syntax looked like this: [@/foo/bar.jpg]. A little digging around lead me to try this syntax: [new CURLFile('/foo/bar.jpg','image/jpeg')]. Sadly while Curl was happy with this, the Facebook PHP SDK (v3.2.2) was not. It turns out the SDK will turn ANY value you send it in that params array into a string.

To get around the issue, he worked up his own fix to the Facebook PHP SDK and submitted a patch to get it introduced into the tool. He also includes a reminder to filter incoming user data for things containing the "@" too to prevent unwanted file transfers.

tagged: curlfile curl file upload facebook sdk patch json

Link: http://philsturgeon.co.uk/blog/2013/08/curlfile-and-the-facebook-sdk-in-php-55

DesignShack.com:
How to Build a Dynamic Imgur Upload App Using jQuery & PHP
Aug 30, 2013 @ 14:51:03

On DesignShack.com Jake Rocheleau has a tutorial showing you how to create an image uploader that pushes the image over to the Imgr service.

In this tutorial I want to demonstrate how we can remotely mirror an image found elsewhere online and auto-upload to Imgur. It’s possible to create a form handling user-uploaded images as well. But I wanted to keep the demo clean without needing to move user content onto the server. This process is very simple once you understand how APIs work.

He provides all of the code and guidance you'll need to get the system working. It uses a simple HTML layout, some jQuery for submitting the image data back to the the server and a PHP script to call the Imgr API. It uses curl to make the call, so you'll need that extension installed to use the example. You can check out the live demo or just download the source to get started quickly.

tagged: imgr upload jquery tutorial javascript api curl

Link: http://designshack.net/articles/javascript/how-to-build-a-dynamic-imgur-upload-app-using-jquery-php/

PHPMaster.com:
Using cURL for Remote Requests
Aug 08, 2013 @ 14:09:13

PHPMaster.com has posted a tutorial showing you how to use the cURL functionality that can be built into PHP. Note that not all PHP installations will have this extension installed, but most will these days. You can find out by making a phpinfo page.

If you’re a Linux user then you’ve probably used cURL. It’s a powerful tool used from posting mails to downloading the latest My Little Pony subtitles. In this article I’ll explain how to use the cURL extension in PHP. The extension offers us the functionality as the console utility in the comfortable world of PHP. I’ll discuss sending GET and POST requests, handling login cookies, and FTP functionality.

He walks thorough the basic flow of a request and how to set options on the cURL handle to modify its behavior. Several more "real world" examples are also included:

  • Retrieve a Web Page
  • Log in to a Website (via POST data, not HTTP Auth)
  • Working with FTP
  • Sending Multiple Requests

That last one changes things up a bit and uses the curl_multi_init function to create the connection and allow for the multiple request streams to happen.

tagged: curl remote request tutorial login ftp multiple

Link: http://phpmaster.com/using-curl-for-remote-requests

VG Tech:
PHP: Perform Requests in Parallel
Jul 23, 2013 @ 15:58:11

On the VG Tech blog today Espen Hovlandsdal has a quick tutorial showing you how to run cURL requests in parallel using the curl_multi_* functions included in PHP.

Ever had to request multiple HTTP-resources in your web application? Often, you need data from one request to be able to request the second – in this case there is little you can do but wait for the first to return. However, if the requests are not dependent on each other, you can use a pretty cool trick: curl_multi_*.

He first gives a single-threat example, showing how you might loop through a set of URLs to make the request and get the response. As an alternative, he shows the "multi" version right after. It sets up a "queue" of handles to different requests and executes them until they stop returning data. He also includes an example using the Guzzle HTTP client that makes it look cleaner and wraps some additional functionality around the requests.

tagged: request parallel curl multiple tutorial guzzle

Link: http://tech.vg.no/2013/07/23/php-perform-requests-in-parallel


Trending Topics: