Examples of using curl and fsockopen are given directly below. You can use them as examples or directly use them as encapsulated functions.
curl function usage code
<code><span>public</span><span><span>function</span><span>xcurl</span><span>(<span>$url</span>,<span>$ref</span>=null,<span>$post</span>=array<span>()</span>,<span>$ua</span>=<span>"Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre"</span>,<span>$print</span>=false)</span> {</span><span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_AUTOREFERER, <span>true</span>); <span>if</span>(!<span>empty</span>(<span>$ref</span>)) { curl_setopt(<span>$ch</span>, CURLOPT_REFERER, <span>$ref</span>); } curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>$url</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>0</span>); curl_setopt(<span>$ch</span>, CURLOPT_FOLLOWLOCATION, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); <span>if</span>(!<span>empty</span>(<span>$ua</span>)) { curl_setopt(<span>$ch</span>, CURLOPT_USERAGENT, <span>$ua</span>); } <span>if</span>(count(<span>$post</span>) > <span>0</span>){ curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$post</span>); } <span>$output</span> = curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>if</span>(<span>$print</span>) { <span>print</span>(<span>$output</span>); } <span>else</span> { <span>return</span><span>$output</span>; } } </code>
fsockopen function usage code:
<code><span>public</span><span><span>function</span><span>curl_request_async</span><span>(<span>$url</span>, <span>$params</span>, <span>$type</span>=<span>'GET'</span>)</span> {</span><span>// set referer</span><span>$referer</span> = <span>$_SERVER</span>[<span>'HTTP_HOST'</span>]; <span>foreach</span> (<span>$params</span><span>as</span><span>$key</span> => &<span>$val</span>) { <span>if</span> (is_array(<span>$val</span>)) <span>$val</span> = implode(<span>','</span>, <span>$val</span>); <span>$post_params</span>[] = <span>$key</span>.<span>'='</span>.urlencode(<span>$val</span>); } <span>$post_string</span> = implode(<span>'&'</span>, <span>$post_params</span>); <span>$parts</span>=parse_url(<span>$url</span>); @<span>$fp</span> = fsockopen(<span>$parts</span>[<span>'host'</span>], <span>isset</span>(<span>$parts</span>[<span>'port'</span>])?<span>$parts</span>[<span>'port'</span>]:<span>80</span>, <span>$errno</span>, <span>$errstr</span>, <span>2</span>); <span>if</span>(!<span>$fp</span>){ <span>echo</span><span>"$errstr ($errno)<br>\n"</span>; }<span>else</span>{ <span>if</span>(<span>'GET'</span> == <span>$type</span>) <span>$parts</span>[<span>'path'</span>] .= <span>'?'</span>.<span>$post_string</span>; <span>$out</span> = <span>"$type "</span>.<span>$parts</span>[<span>'path'</span>].<span>" HTTP/1.1\r\n"</span>; <span>$out</span>.= <span>"Host: "</span>.<span>$parts</span>[<span>'host'</span>].<span>"\r\n"</span>; <span>$out</span>.= <span>"Referer: "</span>.<span>$referer</span>.<span>"\r\n"</span>; <span>$out</span>.= <span>"Content-Type: application/x-www-form-urlencoded\r\n"</span>; <span>$out</span>.= <span>"Connection: Close\r\n\r\n"</span>; <span>// Data goes in the request body for a POST request</span><span>if</span> (<span>'POST'</span> == <span>$type</span> && <span>isset</span>(<span>$post_string</span>)) <span>$out</span>.= <span>$post_string</span>; fwrite(<span>$fp</span>, <span>$out</span>); fclose(<span>$fp</span>); } }</code>
Only from the above functions, the most important difference in usage is:
Curl requests a certain Url and needs to wait for the result to be returned, while fsockopen does not need to return and continues to execute the code after sending the request. Unless you use fsockopen you need to print the results returned after the request.
Copyright Statement: Please indicate the source for reprinting: http://blog.csdn.net/m0sh1
The above has introduced the php curl and fsockopen functions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
