关于php curl支持并发请求,并毫秒控制超时
为什么这么做?
目前的接口话的服务调用,为了保证性能和稳定性,我们都会对调用的第三方接口做并发,超时控制。
代码实现(网上找的现成的)
public static function curlMultiRequest($urls, $options = array()) { $ch= array(); $results = array(); $mh = curl_multi_init(); foreach($urls as $key => $val) { $ch[$key] = curl_init(); if ($options) { curl_setopt_array($ch[$key], $options); } curl_setopt($ch[$key], CURLOPT_URL, $val); curl_multi_add_handle($mh, $ch[$key]); } $running = null; do { curl_multi_exec($mh, $running); } while ($running > 0); // Get content and remove handles. foreach ($ch as $key => $val) { $results[$key] = curl_multi_getcontent($val); curl_multi_remove_handle($mh, $val); } curl_multi_close($mh); return $results; }
调用方式:
$urls = [ 'http://www.baidu.com', 'http://www.qq.com' ];$opts = [ CURLOPT_HEADER => false, CURLOPT_TIMEOUT_MS => 50,//执行脚本超时 //CURLOPT_CONNECTTIMEOUT_MS => 50,//网络选址超时 CURLOPT_RETURNTRANSFER => true, CURLOPT_NOSIGNAL => true, //这个是设定毫秒必须设定];curlMultiRequest($urls,$opts);
注意事项
1.支持毫秒 cURL 7.16.2中被加入。从PHP 5.2.3起可使用
2.CURLOPT_TIMEOUT_MS,CURLOPT_CONNECTTIMEOUT_MS 未定义时
if (!defined('CURLOPT_CONNECTTIMEOUT_MS')) { define('CURLOPT_CONNECTTIMEOUT_MS', 156);}if (!defined('CURLOPT_TIMEOUT_MS')) { define('CURLOPT_TIMEOUT_MS', 155);}
- http://stackoverflow.com/questions/9062798/php-curl-timeout-is-not-working
- http://www.laruence.com/2014/01/21/2939.html
参考资料:

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment
