本文整理汇总了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;
}