【问题标题】:PHP read JSON file From URL (I have a problem with this url)PHP 从 URL 读取 JSON 文件(我的这个 url 有问题)
【发布时间】: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


【解决方案1】:

由于您没有在 return $fileJSON; 之类的问题中使用该函数

这是一个适用于您的代码的示例。

使用固定代码编辑:

<?php

$urlx = "https://www.tmdn.org/tmview/api/trademark/detail/AR500000003159511"; 
echo $urlx."<hr>";


$lurl = getTMview($urlx);
echo '<pre>';

print_r($lurl);

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;

            return $fileJSON;


    }catch(Exception $e) {

        trigger_error(sprintf('Curl falló con error: #%d: %s',$e->getCode(), $e->getMessage()),E_USER_ERROR);
    }
}

这会起作用。

回复:

HTTP/1.1 200 正常 P3P:CP="{}" 设置 Cookie:TSc52fbf36029=0877a508f8ab280078d2853fe8d32985ee08650c11af3bbfb3bba3ae155a7c0b39268e44ca72a94a8d245a5c624d55ac; Max-Age=30;Path=/;samesite=none;Secure P3P:CP="{}" 缓存控制:无存储,必须重新验证,无缓存,max-age=0 内容类型:文本/html 内容长度:4932 P3P:CP="{}" 的Set-Cookie:TSf8346f47027 = 0877a508f8ab2000748180282f1c9f77debce30924021930fe906c1adcd681bcc3f706103710149008c46db52d11300009e3f82113518c847fe985ab6fa69f57beaf40508ecd1e9a33309c342cc372e62d7ff80bdde40d7042a7f91391975530;路径= /; samesite =无;安全 P>

【讨论】:

  • 也许当我输入代码时我编辑了函数但问题仍然存在。函数getTMview($ url){代码}
猜你喜欢
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 2017-05-23
  • 2022-08-18
  • 2012-05-22
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多