Heim  >  Artikel  >  Backend-Entwicklung  >  [优化性能]求高手精简curl的使用配置,该怎么解决

[优化性能]求高手精简curl的使用配置,该怎么解决

WBOY
WBOYOriginal
2016-06-13 10:18:07978Durchsuche

[优化性能]求高手精简curl的使用配置
我有个curl程序,由于是curl到国外某台web service上面,由于操作时间过长,有时候可能需要10秒左右才能完成,我在想能否将curl里的代码精简一下?不必要的去掉。

但是我又不敢乱减,先贴一下我的curl的代码,有没有高手帮忙看看哪几行代码可以去掉不影响使用的?

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        #POST方法        $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方法        $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);


------解决方案--------------------
lz代码已经够精简了,效率问题可能出现在网络或者io上
------解决方案--------------------
我认为你的问题不是精简,而是要加点什么
比如
CURLOPT_USERAGENT
CURLOPT_REFERER

这样才能让对方认为你是一个合法的浏览者,而积极为你服务
------解决方案--------------------

curl_setopt( $curl, CURLOPT_RETURNTRANSFER , 1 );也可以去掉?这个看你具体需要做什么。有的可能有用,有的可能没用
------解决方案--------------------
CURLOPT_HEADER TRUE to include the header in the output.

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

这两个参数是对整个请求的影响是0,只是是否自动输出到标准输出的意思。楼主的代码没有可以精简的了。

网速慢是没有办法解决的,路过。
------解决方案--------------------
加上下面两个,或许可以加速

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.domain.com'));//设置http header。www.domain.com为被你抓取的网站域名。

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");//模拟浏览器
------解决方案--------------------
在美國租個空間來curl美國的內容才是解決問題的根本.就算香港的主機訪問美國也要比大陸強多了.
各種關鍵詞過濾,不慢都慢了.
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn