ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 第十课 curl/memche ``` <pre class="calibre19">``` $cookie <span class="token">=</span><span class="token1">tempnam</span><span class="token2">(</span>__DIR__<span class="token2">,</span><span class="token4">'tmp'</span><span class="token2">)</span><span class="token2">;</span> $ch<span class="token">=</span><span class="token1">curl_init</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span>$url<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_REFERER<span class="token2">,</span> <span class="token4">"http://www.chsi.com.cn/cet/ "</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//来源</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_HEADER<span class="token2">,</span> <span class="token3">0</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//头部head不输出 默认</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_USERAGENT<span class="token2">,</span><span class="token4">'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X)AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H143 MicroMessenger/6.3.9)'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//设置浏览器</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span>CURLOPT_COOKIEFILE<span class="token2">,</span> $cookie<span class="token2">)</span><span class="token2">;</span><span class="token6">//读取cookie</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_POST<span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//设置post数据</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_FOLLOWLOCATION<span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//302跳转</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_POSTFIELDS<span class="token2">,</span> $post<span class="token2">)</span><span class="token2">;</span><span class="token6">//进行post提交</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_RETURNTRANSFER<span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//设置返回字符串不输出 *必须</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_COOKIEJAR<span class="token2">,</span> $cookie<span class="token2">)</span><span class="token2">;</span><span class="token6">//保存cookie</span> $data <span class="token">=</span> <span class="token1">curl_exec</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span><span class="token6">//返回的字符串 *</span> $url<span class="token">=</span> <span class="token1">curl_getinfo</span><span class="token2">(</span>$ch<span class="token2">,</span>CURLINFO_EFFECTIVE_URL<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_close</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span><span class="token6">//关闭</span> 设置SSL支持 https开头必须 <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYPEER<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_SSL_VERIFYHOST<span class="token2">,</span> FALSE<span class="token2">)</span><span class="token2">;</span> ``` ``` curl 上传 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">upimg</span><span class="token2">(</span>$filename<span class="token2">,</span>$type<span class="token">=</span><span class="token4">'image'</span><span class="token2">)</span><span class="token2">{</span> $token <span class="token">=</span> WeAccount<span class="token2">:</span><span class="token2">:</span><span class="token1">token</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $curl <span class="token">=</span> curl_init <span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>class_exists <span class="token2">(</span> <span class="token4">'/CURLFile'</span> <span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span><span class="token6">//php5.5跟php5.6中的CURLOPT_SAFE_UPLOAD的默认值不同 </span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_SAFE_UPLOAD<span class="token2">,</span> <span class="token3">true</span> <span class="token2">)</span><span class="token2">;</span> $msg <span class="token">=</span> array <span class="token2">(</span> <span class="token4">'fieldname'</span> <span class="token">=</span><span class="token">></span> <span class="token5">new</span> <span class="token1"><span class="token2">\</span>CURLFile</span> <span class="token2">(</span> realpath <span class="token2">(</span> $filename <span class="token2">)</span><span class="token2">,</span> <span class="token4">'image/jpeg'</span> <span class="token2">)</span> <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> $msg <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'media'</span><span class="token">=</span><span class="token">></span><span class="token4">'@'</span><span class="token2">.</span>$filename<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>defined <span class="token2">(</span> <span class="token4">'CURLOPT_SAFE_UPLOAD'</span> <span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_SAFE_UPLOAD<span class="token2">,</span> <span class="token3">false</span> <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span> <span class="token4">"https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$token&type=$type"</span><span class="token2">)</span><span class="token2">;</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_SSL_VERIFYPEER<span class="token2">,</span> FALSE <span class="token2">)</span><span class="token2">;</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_SSL_VERIFYHOST<span class="token2">,</span> FALSE <span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span> empty <span class="token2">(</span> $msg <span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_POST<span class="token2">,</span> <span class="token3">1</span> <span class="token2">)</span><span class="token2">;</span> @curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_POSTFIELDS<span class="token2">,</span> $msg <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> curl_setopt <span class="token2">(</span> $curl<span class="token2">,</span> CURLOPT_RETURNTRANSFER<span class="token2">,</span> <span class="token3">1</span> <span class="token2">)</span><span class="token2">;</span> $data <span class="token">=</span> curl_exec <span class="token2">(</span> $curl <span class="token2">)</span><span class="token2">;</span> curl_close <span class="token2">(</span> $curl <span class="token2">)</span><span class="token2">;</span> <span class="token5">return</span> $data<span class="token2">;</span> <span class="token2">}</span> ``` ``` ``` <pre class="calibre14">``` $mem <span class="token">=</span> <span class="token5">new</span> <span class="token1">Memcache</span><span class="token2">;</span> $mem<span class="token">-</span><span class="token">></span><span class="token1">connect</span><span class="token2">(</span><span class="token4">"localhost"</span><span class="token2">,</span> <span class="token3">11211</span><span class="token2">)</span><span class="token2">;</span> $mem<span class="token">-</span><span class="token">></span><span class="token1">set</span><span class="token2">(</span><span class="token4">'key1'</span><span class="token2">,</span> <span class="token4">'这是值'</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> <span class="token3">60</span><span class="token2">)</span><span class="token2">;</span> $val <span class="token">=</span> $mem<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'key1'</span><span class="token2">)</span><span class="token2">;</span> echo <span class="token4">"输出: "</span> <span class="token2">.</span> $val<span class="token2">;</span> $mem<span class="token">-</span><span class="token">></span><span class="token1">close</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $mem1 <span class="token">=</span> <span class="token1">memcache_connect</span><span class="token2">(</span><span class="token4">'localhost'</span><span class="token2">,</span><span class="token3">11211</span><span class="token2">)</span><span class="token2">;</span> $mem1<span class="token">-</span><span class="token">></span><span class="token1">set</span><span class="token2">(</span><span class="token4">'a'</span><span class="token2">,</span><span class="token4">'1'</span><span class="token2">)</span><span class="token2">;</span> echo $mem1<span class="token">-</span><span class="token">></span><span class="token1">get</span><span class="token2">(</span><span class="token4">'a'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">memcache_close</span><span class="token2">(</span>$mem1<span class="token2">)</span><span class="token2">;</span> ``` ``` ``` <pre class="calibre16">``` $cookie <span class="token">=</span> <span class="token1">tempnam</span><span class="token2">(</span>__DIR__<span class="token2">,</span><span class="token4">'tmp'</span><span class="token2">)</span><span class="token2">;</span> $url <span class="token">=</span> <span class="token4">"http://tp5.api.shanliwawa.top/admin/index/login"</span><span class="token2">;</span> $arr<span class="token">=</span> <span class="token2">[</span> <span class="token4">'username'</span><span class="token">=</span><span class="token">></span><span class="token4">"admin"</span><span class="token2">,</span><span class="token4">"password"</span><span class="token">=</span><span class="token">></span><span class="token4">'123456'</span> <span class="token2">]</span><span class="token2">;</span> $ch1<span class="token">=</span><span class="token1">curl_init</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span>$url<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_HEADER<span class="token2">,</span> <span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_POST<span class="token2">,</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//设置post数据</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_FOLLOWLOCATION<span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//302跳转</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_POSTFIELDS<span class="token2">,</span><span class="token1">http_build_query</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//进行post提交</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_RETURNTRANSFER<span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//设置返回字符串不输出 *必须</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch1<span class="token2">,</span> CURLOPT_COOKIEJAR<span class="token2">,</span> $cookie<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_exec</span><span class="token2">(</span>$ch1<span class="token2">)</span><span class="token2">;</span><span class="token6">//返回的字符串 *</span> $info <span class="token">=</span> <span class="token1">curl_getinfo</span><span class="token2">(</span>$ch1<span class="token2">,</span>CURLINFO_EFFECTIVE_URL<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_close</span><span class="token2">(</span>$ch1<span class="token2">)</span><span class="token2">;</span><span class="token6">//关闭</span> $arrs <span class="token">=</span> <span class="token1">parse_url</span><span class="token2">(</span>$url<span class="token2">)</span><span class="token2">;</span> $url2 <span class="token">=</span> <span class="token4">"http://"</span><span class="token2">.</span>$arrs<span class="token2">[</span><span class="token4">'host'</span><span class="token2">]</span><span class="token2">.</span><span class="token4">"/admin/index/index"</span><span class="token2">;</span> $ch<span class="token">=</span><span class="token1">curl_init</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_URL<span class="token2">,</span>$url2<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span>CURLOPT_COOKIEFILE<span class="token2">,</span> $cookie<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_setopt</span><span class="token2">(</span>$ch<span class="token2">,</span> CURLOPT_HEADER<span class="token2">,</span> <span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> $data <span class="token">=</span> <span class="token1">curl_exec</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token1">curl_close</span><span class="token2">(</span>$ch<span class="token2">)</span><span class="token2">;</span> <span class="token1">unlink</span><span class="token2">(</span>$cookie<span class="token2">)</span><span class="token2">;</span> <span class="token1">exit</span><span class="token2">(</span>$data<span class="token2">)</span><span class="token2">;</span> ``` ```