php curlopt httpget,php http请求 curl方法 curl http get curl php curlopt httpheade

host = $host;

}

/**

* GET wrappwer for request.

*

* @return mixed

*/

function get($url, $parameters = array(), $headers = array(),$cookie = array()) {

$response = $this->request($url, 'GET', $parameters, NULL, $headers,$cookie);

if ($this->format === 'json' && $this->decode_json) {

return json_decode($response, true);

}

return $response;

}

/**

* POST wreapper for request.

*

* @return mixed

*/

function post($url, $parameters = array(), $multi = false, $headers = array(),$cookie = array()) {

$response = $this->request($url, 'POST', $parameters, $multi, $headers,$cookie );

if ($this->format === 'json' && $this->decode_json) {

return json_decode($response, true);

}

return $response;

}

/**

* DELTE wrapper for oAuthReqeust.

*

* @return mixed

*/

function delete($url, $parameters = array()) {

$response = $this->request($url, 'DELETE', $parameters);

if ($this->format === 'json' && $this->decode_json) {

return json_decode($response, true);

}

return $response;

}

/**

* Format and sign an OAuth / API request

*

* @return string

* @ignore

*/

function request($url, $method, $parameters, $multi = false, $headers = array(),$cookie = array()) {

if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {

$url = "{$this->host}{$url}";

}

switch ($method) {

case 'GET':

$url .= strpos($url, '?') === false ? '?' : '';

$url .= http_build_query($parameters);

return $this->http($url, 'GET', null, $headers, $cookie);

default:

//$headers = array();

$body = $parameters;

if($multi)

{

$body = self::build_http_query_multi($parameters);

$headers[] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;

}

elseif(is_array($parameters) || is_object($parameters))

{

if(in_array('Content-Type: application/json',$headers) || in_array('Content-Type:application/json',$headers))

{

$body = json_encode($parameters);

}

else

{

$body = http_build_query($parameters);

}

}

return $this->http($url, $method, $body, $headers, $cookie);

}

}

/**

* Make an HTTP request

*

* @return string API results

* @ignore

*/

function http($url, $method, $postfields = NULL, $headers = array() ,$cookie = array()) {

$this->http_info = array();

$ci = curl_init();

/* Curl settings */

curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);

curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);

curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);

curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ci, CURLOPT_ENCODING, "");

curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);

curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));

curl_setopt($ci, CURLOPT_HEADER, FALSE);

switch ($method) {

case 'POST':

curl_setopt($ci, CURLOPT_POST, TRUE);

if (!empty($postfields)) {

curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

$this->postdata = $postfields;

}

break;

case 'DELETE':

curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');

if (!empty($postfields)) {

$url = "{$url}?{$postfields}";

}

}

$headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];

curl_setopt($ci, CURLOPT_URL, $url );

curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );

curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

if($this->referer)

{

curl_setopt($ci, CURLOPT_REFERER, $this->referer);

}

if ($this->follow)

{

curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);

}

if(!empty($cookie))

{

$str = "";

foreach ($cookie as $key=>$value)

{

$str.="{$key}={$value};";

}

$str = trim($str,';');

curl_setopt($ci, CURLOPT_COOKIE, $str);

}

$response = curl_exec($ci);

$this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

$this->http_info = array_merge($this->http_info, curl_getinfo($ci));

if ($this->http_code != 200)

{

throw new Exception(json_encode($this->http_info), $this->http_code);

}

$this->url = $url;

curl_close ($ci);

return $response;

}

/**

* Get the header info to store.

*

* @return int

* @ignore

*/

function getHeader($ch, $header) {

$i = strpos($header, ':');

if (!empty($i)) {

$key = str_replace('-', '_', strtolower(substr($header, 0, $i)));

$value = trim(substr($header, $i + 2));

$this->http_header[$key] = $value;

}

return strlen($header);

}

/**

* 处理多媒体数据内容

* @ignore

*/

public static function build_http_query_multi($params) {

if (!$params) return '';

uksort($params, 'strcmp');

$pairs = array();

self::$boundary = $boundary = uniqid('------------------');

$MPboundary = '--'.$boundary;

$endMPboundary = $MPboundary. '--';

$multipartbody = '';

foreach ($params as $parameter => $value) {

if( in_array($parameter, array('pic', 'image','Filedata')) && $value{0} == '@' ) {

$url = ltrim( $value, '@' );

$content = file_get_contents( $url );

$array = explode( '?', basename( $url ) );

$filename = $array[0];

$multipartbody .= $MPboundary . "\r\n";

$multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"'. "\r\n";

$multipartbody .= "Content-Type: image/unknown\r\n\r\n";

$multipartbody .= $content. "\r\n";

} else {

$multipartbody .= $MPboundary . "\r\n";

$multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";

$multipartbody .= $value."\r\n";

}

}

$multipartbody .= $endMPboundary;

return $multipartbody;

}

/**

* 批量请求urlget

* Enter description here ...

* @param unknown_type $urls

* @return array

*/

public function batch_get($url_arr)

{

$mh = curl_multi_init();

foreach ($url_arr as $i => $url)

{

$conn[$i]=curl_init($url);

curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);//设置返回do.php页面输出内容

curl_multi_add_handle ($mh,$conn[$i]);//添加线程

}

do

{

$mrc = curl_multi_exec($mh,$active);

}

while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active and $mrc == CURLM_OK)

{

if (curl_multi_select($mh) != -1) {

do

{

$mrc = curl_multi_exec($mh, $active);

}

while($mrc == CURLM_CALL_MULTI_PERFORM);

}

}

foreach ($url_arr as $i => $url)

{

$res[$i]=curl_multi_getcontent($conn[$i]);//得到页面输入内容

curl_close($conn[$i]);

}

return $res;

}

}

以上就介绍了php http请求 curl方法,包括了curl,http方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值