[Performance optimization] how to streamline the use configuration of curl?

Source: Internet
Author: User
[Performance optimization] I have a curl program to help you streamline the use and configuration of curl. because curl is sent to a webservice outside China, sometimes it may take about 10 seconds to complete the operation because the operation is too long, I was wondering if I could streamline the code in curl? Remove unnecessary. But I do not dare to undo it. first paste my curl code, and I want to [optimize the performance] to streamline the usage configuration of curl.
I have a curl program, because it is a curl to a foreign web service, because the operation is too long, sometimes it may take about 10 seconds to complete, I was wondering if I could streamline the code in curl? Remove unnecessary.

But I am afraid to undo it again. I will first paste my curl code. Can anyone help me to see which lines of code can be removed without affecting the usage?

PHP code
  # POST method $ curl = curl_init ($ url); curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); curl_setopt ($ curl, CURLOPT_HEADER, 0); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); $ response = curl_exec ($ curl); # GET method $ curl = curl_init ($ url); curl_setopt ($ curl, CURLOPT_GET, 1); curl_setopt ($ curl, CURLOPT_GETFIELDS); curl_setopt ($ curl, CURLOPT_HEADER, 0); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); $ response = curl_exec ($ curl );


------ Solution --------------------
The lz code is simplified enough, and the efficiency problem may occur on the network or I/O.
------ Solution --------------------
I think your problem is not simplified, but something to add.
For example
CURLOPT_USERAGENT
CURLOPT_REFERER

In this way, the other party can regard you as a valid viewer and actively serve you
------ Solution --------------------

Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); can be removed? It depends on what you need to do. Some may be useful, and some may be useless.
------ Solution --------------------
CURLOPT_HEADERTRUE to include the header in the output.

CURLOPT_RETURNTRANSFERTRUE to return the transfer as a string of the return value of curl_exec () instead of outputting it out directly.

The two parameters affect the entire request by 0, but they only mean whether to automatically output to the standard output. The landlord's code cannot be simplified.

There is no way to solve the problem of slow network speed.
------ Solution --------------------
Add the following two to accelerate

Curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ('host: www.domain.com '); // sets the http header. Www.domain.com is the domain name of the website you crawled.

Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); // simulate a browser
------ Solution --------------------
Curl is the root solution to the US challenge. even if the Hong Kong host asks the US, it is much larger.
Various words are too slow.

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.