Php can establish a connection only once to obtain remote content and use it multiple times. Solution-php Tutorial

Source: Internet
Author: User
Php can establish a connection only once to obtain remote content and use it multiple times. example: & nbsp; $ chcurl_init (); & nbsp; $ timeout30; & nbsp; curl_setopt ($ ch, CURLOPT_URL, $ url); & nbsp; curl_setopt ($ ch, CU php can obtain remote content, establish a connection only once, and use it multiple times.
For example:
$ Ch = curl_init ();
$ Timeout = 30;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
$ Contents = trim (curl_exec ($ ch ));
Curl_close ($ ch );

The url of the target address is stable and only the parameters passed are different,
Can the code contain only
$ Timeout = 30;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
$ Contents = trim (curl_exec ($ ch ));
You do not need to create a connection or close a connection each time.

------ Solution --------------------
CURLOPT_CUSTOMREQUEST
------ Solution --------------------
PHP code
[User: root Time: 14: 07: 31 Path:/home/liangdong/php] $ php file. php HTTP/1.1 200 OKDate: Thu, 10 May 2012 18:07:32 GMTServer: Apache/2.4.1 (Unix) PHP/5.4.0X-Powered-By: PHP/5.4.0Set-Cookie: PHPSESSID = qi60b6s1q783a4pnj81rscdet4; path =/Expires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check = 0, pre-check = 0 Pragma: no-cacheContent-Length: 257Keep-Alive: timeout = 5, max = 100 Connection: Keep-AliveContent-Type: text/html; charset = utf-8

^_^ Hello, welcome to ThinkPHP

GET/HTTP/1.1 Host: localhostAccept: */* Connection: Keep-alive [User: root Time: 14: 07: 32 Path: /home/liangdong/php] $ cat file. php [User: root Time: 14: 07: 43 Path:/home/liangdong/php] $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.