本文整理匯總了PHP中CUrl::setOptions方法的典型用法代碼示例。如果您正苦於以下問題:PHP CUrl::setOptions方法的具體用法?PHP CUrl::setOptions怎麽用?PHP CUrl::setOptions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類CUrl
的用法示例。
在下文中一共展示了CUrl::setOptions方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: postUploadFile
/**
* @example
* $content = array(
* 'foo' => 'bar',
* 'file' => array(
* 'content' => 'file content is simple text', // or path, mantatory
* 'name' => 'filename.txt', // optional
* 'type' => 'text/plain' // optional
* ));
*
* @param string $url
* @param array $content
* @return CUrl - call ->exec()
*/
static function postUploadFile($url, array $content)
{
$eol = "\r\n";
$boundary = md5(microtime(TRUE));
$curl = new CUrl($url);
$curl->setOptions(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_VERBOSE => 1, CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data; charset=utf-8; boundary="' . $boundary . '"')));
$body = '';
foreach ($content as $name => $value) {
$body .= '--' . $boundary . $eol;
$body .= 'Content-Disposition: form-data;name="' . $name . '"';
if (is_array($value)) {
if (file_exists($value['content'])) {
$type = MimeTypeDetector::fromFile($value['content']);
$content = file_get_contents($value['content']);
} else {
$type = MimeTypeDetector::fromString($content = $value['content']);
}
// is file
$body .= '; filename="' . (isset($value['name']) ? $value['name'] : date('YmdHis')) . '"' . $eol;
$body .= 'Content-Type: ' . (isset($value['type']) ? $value['type'] : $type) . $eol;
if (preg_match('~base64~i', $content)) {
$body .= 'Content-Transfer-Encoding: base64' . $eol;
$content = preg_replace('~^base64~i', '', $content);
}
$body .= $eol;
// $body .= chunk_split(base64_encode($content)); // RFC 2045
$body .= trim($content) . $eol;
} else {
$body .= $eol . $eol . $value . $eol;
}
}
$body .= "--{$boundary}--" . $eol . $eol;
$curl->setopt(CURLOPT_POSTFIELDS, $body);
return $curl;
}