file_get_contents版本:
01 |
<?php |
02 |
/** |
03 |
* Send post request
|
04 |
* @param string $url request address
|
05 |
* @param array $post_data post key-value pair data
|
06 |
* @return string
|
07 |
*/
|
08 |
function send_post( $url ,
$post_data ) {
|
09 |
10 |
$postdata = http_build_query( $post_data );
|
11 |
$options = array (
|
12 |
'http' => array (
|
13 |
'method' => 'POST' ,
|
14 |
'header' => 'Content-type:application/x-www-form-urlencoded' ,
|
15 |
'content' => $postdata ,
|
16 |
'timeout' => 15 * 60 // 超时时间(单位:s)
|
17 |
)
|
18 |
);
|
19 |
$context = stream_context_create( $options );
|
20 |
$result = file_get_contents ( $url , false,
$context );
|
21 |
22 |
return $result ;
|
23 |
} |
使用如下:
1 |
$post_data = array (
|
2 |
'username' => 'stclair2201' ,
|
3 |
'password' => 'handan'
|
4 |
); |
5 |
send_post( 'http://blog.snsgou.com' ,
$post_data );
|
Practical experience:
When I used the above code to send an http request to another server, I found that if the server takes too long to process the request, the local PHP will interrupt the request, which is the so-called timeout interrupt. The first one I suspect that the execution time of PHP itself exceeds the limit, but I shouldn’t think about it because I have set the “PHP execution time limit” according to this article a long time ago ([Recommended] PHP upload file size limit list). After careful consideration, I thought I thought it should be a time limit on the http request itself, so I thought of how to increase the time limit on the http request. . . . . . Check the PHP manual, and there is indeed a parameter "timeout", I don't know how big it is by default. When you set its value to a larger value, the problem will be solved. Let me make a note~~~
Socket version:
01 |
/** |
02 |
* Socket version
|
03 |
* How to use:
|
04 |
* $post_string = "app=socket&version=beta";
|
05
|
* request_by_socket( 'blog.snsgou.com', '/restServer.php', $post_string);
|
06 |
*/
|
07 |
function request_by_socket( $remote_server , $remote_path , $ post_string , $port = 80, $timeout = 30) {
|
08 |
$socket = fsockopen ( $remote_server ,
$port , $errno ,
$errstr , $timeout );
|
09 |
if (! $socket )
die ( "$errstr($errno)" );
|
10
|
fwrite( $socket ,
"POST $remote_path HTTP/1.0" );
|
$socket | ,
"User-Agent: Socket Example" );
|
"HOST: $remote_server" | );
13
|
"Content-type: application/x-www-form-urlencoded" | );
$socket ,
"Content-length: " . ( strlen (
| $post_string
|
15
"Accept:*/*" );
| 16
,
| ""
);
| 17
|
18
| fwrite(
|
19
| $header
Curl版本:
Curl版本(2)
,
'POST'
25
调用 WCF接口 的一个例子:$json = restRequest($r_url,'POST', json_encode($data)); 以上就介绍了php实现post和get,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
|

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

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


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

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
