php curl用法

在php使用curl时必须先在php.ini中开启extension=php_curl.dll前面的;去了才行,curl是php中一款内置的浏览器,它可以模仿用户浏览信息进行网站浏览,等下面来看一实例,代码如下:

  1. function postpage($url)
  2. {
  3. $response = "";
  4. $rd=rand(1,4);
  5. $proxy='http://221.214.27.253:808';
  6. if($rd==2) $proxy='http://222.77.14.56:8088';
  7. if($rd==3) $proxy='http://202.98.123.126:8080';
  8. if($rd==4) $proxy='http://60.14.97.38:8080';
  9. if($url != "") {
  10. $ch = curl_init($url);
  11. curl_setopt($ch, curlopt_header, 0);
  12. curl_setopt($ch, curlopt_returntransfer, true);
  13. curl_setopt($ch, curlopt_proxy, $proxy);
  14. $response = curl_exec($ch);
  15. if(curl_errno($ch)) $response = "";
  16. curl_close($ch);
  17. }
  18. return $response;
  19. }

curl包括部份函数,如下所示:

  1. curl_close — close a curl session
  2. curl_copy_handle — copy a curl handle along with all of its preferences
  3. curl_errno — return the last error number
  4. curl_error — return a string containing the last error for the current session
  5. curl_exec — perform a curl session
  6. curl_getinfo — get information regarding a specific transfer
  7. curl_init — initialize a curl session
  8. curl_multi_add_handle — add a normal curl handle to a curl multi handle
  9. curl_multi_close — close a set of curl handles
  10. curl_multi_exec — run the sub-connections of the current curl handle
  11. curl_multi_getcontent — return the content of a curl handle if curlopt_returntransfer is set
  12. curl_multi_info_read — get information about the current transfers
  13. curl_multi_init — returns a new curl multi handle
  14. curl_multi_remove_handle — remove a multi handle from a set of curl handles
  15. curl_multi_select — wait for activity on any curl_multi connection
  16. curl_setopt_array — set multiple options for a curl transfer
  17. curl_setopt — set an option for a curl transfer
  18. curl_version — gets curl version information