php http请求 curl方法 curl http get curl php curlopt httpheade
<?php /** * * @brief http请求类 * **/ class Activity_Http { /** * Contains the last HTTP status code returned. */ public $http_code; /** * Contains the last API call. */ public $url; /** * Set up the API root URL. */ public $host; /** * Set timeout default. */ public $timeout = 10; /** * Set connect timeout. */ public $connecttimeout = 10; /** * Respons format. */ public $format = 'unknow'; /** * Decode returned json data. */ public $decode_json = TRUE; /** * Contains the last HTTP headers returned. */ public $http_info; /** * print the debug info */ public $debug = FALSE; /** * Verify SSL Cert. */ public $ssl_verifypeer = FALSE; /** * Set the useragnet. */ public $useragent = 'xxx'; /** * 模拟Referer * @var url */ public $referer; public $follow; /** * boundary of multipart * @ignore */ public static $boundary = ''; public function __construct($host = '') { $this->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教程有兴趣的朋友有所帮助。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

http状态码520是指服务器在处理请求时遇到了一个未知的错误,无法提供更具体的信息。用于表示服务器在处理请求时发生了一个未知的错误,可能是由于服务器配置问题、网络问题或其他未知原因导致的。通常是由服务器配置问题、网络问题、服务器过载或代码错误等原因导致的。如果遇到状态码520错误,最好联系网站管理员或技术支持团队以获取更多的信息和帮助。

在Linux下更新curl版本,您可以按照以下步骤进行操作:检查当前curl版本:首先,您需要确定当前系统中安装的curl版本。打开终端,并执行以下命令:curl--version该命令将显示当前curl的版本信息。确认可用的curl版本:在更新curl之前,您需要确定可用的最新版本。您可以访问curl的官方网站(curl.haxx.se)或相关的软件源,查找最新版本的curl。下载curl源代码:使用curl或浏览器,下载您选择的curl版本的源代码文件(通常为.tar.gz或.tar.bz2

掌握HTTP301状态码的含义:网页重定向的常见应用场景随着互联网的迅猛发展,人们对网页交互的要求也越来越高。在网页设计领域,网页重定向是一种常见且重要的技术,通过HTTP301状态码来实现。本文将探讨HTTP301状态码的含义以及在网页重定向中的常见应用场景。HTTP301状态码是指永久重定向(PermanentRedirect)。当服务器接收到客户端发

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

如何使用NginxProxyManager实现HTTP到HTTPS的自动跳转随着互联网的发展,越来越多的网站开始采用HTTPS协议来加密传输数据,以提高数据的安全性和用户的隐私保护。由于HTTPS协议需要SSL证书的支持,因此在部署HTTPS协议时需要有一定的技术支持。Nginx是一款强大且常用的HTTP服务器和反向代理服务器,而NginxProxy

http状态码403是服务器拒绝了客户端的请求的意思。解决http状态码403的方法是:1、检查身份验证凭据,如果服务器要求身份验证,确保提供正确的凭据;2、检查IP地址限制,如果服务器对IP地址进行了限制,确保客户端的IP地址被列入白名单或未列入黑名单;3、检查文件权限设置,如果403状态码与文件或目录的权限设置有关,确保客户端具有足够的权限访问这些文件或目录等等。

PHPCurl中如何处理网页的301重定向?在使用PHPCurl发送网络请求时,时常会遇到网页返回的301状态码,表示页面被永久重定向。为了正确处理这种情况,我们需要在Curl请求中添加一些特定的选项和处理逻辑。下面将详细介绍在PHPCurl中如何处理网页的301重定向,并提供具体的代码示例。301重定向处理原理301重定向是指服务器返回了一个30

解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。
