【发布时间】:2021-11-06 21:27:48
【问题描述】:
我正在尝试这个。
php
$urlx = "https://www.tmdn.org/tmview/api/trademark/detail/AR500000003159511";
echo $urlx."<hr>";
$lurl = getTMview($urlx);
function getTMview($url) {
$agent= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:92.0) Gecko/20100101 Firefox/92.0";
try {
$request_headers = [
'Content-Type: application/json; charset=UTF-8',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
];
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath('COOKIE_FILE'));
curl_setopt($curl, CURLOPT_COOKIE, $strCookie);
curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($curl, CURLOPT_MAXREDIRS , 20);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLPROTO_HTTPS, true);
$curl_response = curl_exec($curl);
$redirectedUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
if ($curl_response === false) {
throw new Exception(curl_error($curl), curl_errno($curl));
}else {
$curl_response;
}
curl_close($curl);
$fileJSON = $curl_response;
echo "<pre>"; print_r($fileJSON);
}catch(Exception $e) {
trigger_error(sprintf('Curl falló con error: #%d: %s',$e->getCode(), $e->getMessage()),E_USER_ERROR);
}
}
?>
===========
但是我无法获得 json 响应,但是如果我将 url 复制并粘贴到浏览器中,如果它显示它。我在 response 中得到的是这个,但没有错误。
======
https://www.tmdn.org/tmview/api/trademark/detail/AR500000003159511
HTTP/1.1 200 正常 P3P:CP="{}" 设置 Cookie:TSc52fbf36029=0877a508f8ab28002302a94e31ca1ca3f9f80e8d60adfbeae9bc3ab0119554a8c2d27857b7fa39b02e1dc40e4243b162; Max-Age=30;Path=/;samesite=none;Secure P3P:CP="{}" 缓存控制:无存储,必须重新验证,无缓存,max-age=0 内容类型:文本/html 内容长度:5430 P3P:CP="{}" 的Set-Cookie:TSf8346f47027 = 0877a508f8ab2000a016ba36910d303990b09e456f92087e394a1bbfc51e3a3ed5983a3e8a799e5b080d3caf0b113000af09061e611dcfc0fa67e9c30833e3320f96cd04d86058a6dea70d934ae9a9ef600b4f75275fbd81322ffbf1a139e9e6;路径= /; samesite =无;安全 P>
=======
感谢您的帮助!!!
【问题讨论】:
-
确定这是您的正确代码吗?你的函数需要两个参数,你只传递一个
-
你是对的,它只有1个参数,但如果你尝试它,那不是错误。也许我已经修改了它。谢谢
标签: php json curl web-scraping php-curl