php自定义函数: 改进升级版curl
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php自定义函数: 改进升级版curl相关的知识,希望对你有一定的参考价值。
<?php function dcurl($url, $method = ‘GET‘, $postFields = null, $header = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } switch ($method) { case ‘POST‘: curl_setopt($ch, CURLOPT_POST, true); if (!empty($postFields)) { if (is_array($postFields) || is_object($postFields)) { if (is_object($postFields)) $postFields = Tools::object2array($postFields); $postBodyString = ""; $postMultipart = false; foreach ($postFields as $k => $v) { if ("@" != substr($v, 0, 1)) { //判断是不是文件上传 $postBodyString .= "$k=" . urlencode($v) . "&"; } else { //文件上传用multipart/form-data,否则用www-form-urlencoded $postMultipart = true; } } unset($k, $v); if ($postMultipart) { curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1)); } } else { curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } } break; default: if (!empty($postFields) && is_array($postFields)) $url .= (strpos($url, ‘?‘) === false ? ‘?‘ : ‘&‘) . http_build_query($postFields); break; } } ?>
;
以上是关于php自定义函数: 改进升级版curl的主要内容,如果未能解决你的问题,请参考以下文章