PHP cURL 获取重定向 URL 并记录请求响应:解决 $data['url'] 为空的问题
{/"title/":/"function geturl(/$url){//n /$ch = curl_init(/$url);//n //n // 设置请求头//n /$headers = [//n 'Host: api.huoshan.com',//n 'Connection: keep-alive',//n 'Cache-Control: max-age=0',//n 'Upgrade-Insecure-Requests: 1',//n 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',//n 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7',//n 'Sec-Fetch-Site: none',//n 'Sec-Fetch-Mode: navigate',//n 'Sec-Fetch-User: ?1',//n 'Sec-Fetch-Dest: document',//n 'sec-ch-ua: /'Not/A)Brand/';v=/'99/', /'Google Chrome/';v=/'115/', /'Chromium/';v=/'115/'',//n 'sec-ch-ua-mobile: ?0',//n 'sec-ch-ua-platform: /'Windows/'',//n 'Accept-Encoding: gzip, deflate, br',//n 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ga;q=0.6'//n ];//n //n curl_setopt(/$ch, CURLOPT_HTTPHEADER, /$headers);//n curl_setopt(/$ch, CURLOPT_HEADER, true);//n curl_setopt(/$ch, CURLOPT_FOLLOWLOCATION, false);//n curl_setopt(/$ch, CURLOPT_RETURNTRANSFER, true);//n /$response = curl_exec(/$ch);//n //n // 获取重定向后的地址//n /$redirectUrl = '';//n //n if (preg_match('/Location: (.+)/', /$response, /$matches)) {//n /$redirectUrl = trim(/$matches[1]);//n }//n curl_close(/$ch);//n return /$redirectUrl;//n}//n,php中,/$data['url'] = geturl(/$data['url']);这一步中/$data['url']的值可能为空,这是为什么?应该如何解决?同时使function geturl(/$url)函数多输出一个变量/$response,并赋值给/$data['log'],以查清原因内容:在函数geturl(/$url)中,如果/$data['url']的值为空,可能是由于以下几种原因://n//n1. /$url参数传递给geturl()函数的值为空。//n2. curl_init()函数无法正确初始化cURL会话。//n3. curl_exec()函数无法成功执行cURL请求。//n//n为了解决这个问题,可以进行以下步骤://n//n1. 确保/$data['url']的值不为空,可以在调用geturl()函数之前进行检查。//n2. 检查curl_init()函数是否返回false,如果是,则可能是由于PHP未启用cURL扩展或URL格式不正确。可以在curl_init()函数之后添加以下代码进行检查://nphp//nif (!/$ch) {//n /$data['log'] = 'cURL initialization failed';//n return /$redirectUrl;//n}//n//n3. 检查curl_exec()函数是否返回false,如果是,则可能是由于请求失败或未收到响应。可以在curl_exec()函数之后添加以下代码进行检查://nphp//nif (!/$response) {//n /$data['log'] = 'cURL execution failed';//n return /$redirectUrl;//n}//n//n4. 在geturl()函数中添加以下代码,将/$response赋值给/$data['log']://nphp//n/$data['log'] = /$response;//n//n完整的修改后的函数代码如下://nphp//nfunction geturl(/$url){//n /$ch = curl_init(/$url);//n //n // 设置请求头//n /$headers = [//n 'Host: api.huoshan.com',//n 'Connection: keep-alive',//n 'Cache-Control: max-age=0',//n 'Upgrade-Insecure-Requests: 1',//n 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',//n 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',//n 'Sec-Fetch-Site: none',//n 'Sec-Fetch-Mode: navigate',//n 'Sec-Fetch-User: ?1',//n 'Sec-Fetch-Dest: document',//n 'sec-ch-ua: /'Not/A)Brand/';v=/'99/', /'Google Chrome/';v=/'115/', /'Chromium/';v=/'115/'',//n 'sec-ch-ua-mobile: ?0',//n 'sec-ch-ua-platform: /'Windows/'',//n 'Accept-Encoding: gzip, deflate, br',//n 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,ga;q=0.6'//n ];//n //n curl_setopt(/$ch, CURLOPT_HTTPHEADER, /$headers);//n curl_setopt(/$ch, CURLOPT_HEADER, true);//n curl_setopt(/$ch, CURLOPT_FOLLOWLOCATION, false);//n curl_setopt(/$ch, CURLOPT_RETURNTRANSFER, true);//n /$response = curl_exec(/$ch);//n //n // 获取重定向后的地址//n /$redirectUrl = '';//n //n if (preg_match('/Location: (.+)/', /$response, /$matches)) {//n /$redirectUrl = trim(/$matches[1]);//n }//n curl_close(/$ch);//n //n /$data['log'] = /$response;//n return /$redirectUrl;//n}//n//n请注意,修改后的代码将/$data['log']添加到了函数的返回值中,因此在调用geturl()函数时,需要将返回值分别赋给/$data['url']和/$data['log']://nphp//n/$data['url'] = geturl(/$data['url']);//n/$data['log'] = geturl(/$data['url']);//n//n这样就可以通过/$data['log']查看/$response的值,以确定为什么/$data['url']可能为空。
原文地址: https://www.cveoy.top/t/topic/pZVa 著作权归作者所有。请勿转载和采集!