代码之家  ›  专栏  ›  技术社区  ›  Andrew

Zend_uri_http:使用Google Chart URI时提供的URI无效?

  •  0
  • Andrew  · 技术社区  · 14 年前

    我的应用程序使用谷歌图表和HTTPS。我需要将谷歌图表显示为“安全”图像,否则Internet Explorer会抱怨显示不安全的内容。因此,我正在尝试使用一个 zend_http_client request下载它们(然后链接到本地文件),但我似乎无法做到。

    URI应该有效,因为我可以单击此链接并查看图像:

    http://chart.apis.google.com/chart?CHS=250x150&cht=bvg&chd s=0,19&chd=t:19&chxt=x,y&chxl=0:2009&chxr=1,0,19&chf=bg,s,f2f0e1

    这是我使用的代码:

    $chartURL='http://chart.apis.google.com/chart?CHS=250x150&cht=bvg&chd s=0,19&chd=t:19&chxt=x,y&chxl=0:2009&chxr=1,0,19&chf=bg,s,f2f0e1';
    $client=new zend_http_client($chartURL,array('maxRedirects'=>0,'timeout'=>30));
    $response=$client->请求();
    < /代码> 
    
    

    我做错什么了?我还有别的方法可以做到吗?

    解决

    由于google chart uri使用“无效”字符,因此在构造zend_uri时,它无法通过验证。这是我下载谷歌图表所必须做的。

    <预> <代码> /** *返回已下载并存储在本地的谷歌图表图像的URL。如果尚未下载,将进行下载。 *@参数字符串$chartURL *@返回字符串 */ 受保护的函数\u GetLocalImageURL($chartURL) { $savePath=realpath(应用程序路径'/../public/resources/google charts/'); $hashedcharturl=md5($charturl); $localPath=“$savePath/$hashedchartURL”; 如果(!)文件存在($localpath))。{ exec(“wget-o\”$localpath\“\”$charturl\“”); } return“/resources/google charts/$hashedcharturl”; } < /代码> <因为我可以单击此链接并查看图像,所以ld是有效的:

    http://chart.apis.google.com/chart?CHS=250x150&cht=bvg&chd s=0,19&chd=t:19&chxt=x,y&chxl=0:2009&chxr=1,0,19&chf=bg,s,f2f0e1

    以下是我使用的代码:

    $chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t:19&chxt=x,y&chxl=0:|2009&chxr=1,0,19&chf=bg,s,F2F0E1';
    $client = new Zend_Http_Client($chartUrl, array('maxredirects' => 0, 'timeout' => 30));
    $response = $client->request();
    

    我做错什么了?我还有别的方法可以做到吗?

    周围工作

    由于google chart uri使用“无效”字符,因此在构造Zend_Uri. 这是我下载谷歌图表所必须做的。

    /**
     * Returns the URL of the Google chart image that has been downloaded and stored locally. If it has not been downloaded yet, it will be download.
     * @param string $chartUrl
     * @return string
     */
    protected function _getLocalImageUrl($chartUrl)
    {
        $savePath = realpath(APPLICATION_PATH . '/../public/Resources/google-charts/');
        $hashedChartUrl = md5($chartUrl);
        $localPath = "$savePath/$hashedChartUrl";
    
        if (!file_exists($localPath)) {
            exec("wget -O \"$localPath\" \"$chartUrl\"");
        }
    
        return "/Resources/google-charts/$hashedChartUrl";
    }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Andrei Serdeliuc ॐ    14 年前

    尝试: $chartUrl = 'http://chart.apis.google.com/chart?chs=250x150&cht=bvg&chds=0,19&chd=t%3A19&chxt=x,y&chxl=0%3A|2009&chxr=1,0,19&chf=bg,s,F2F0E1';

    [编辑] 正如评论中提到的,urlencode没有解决这个问题,但是用十六进制值替换冒号的确解决了问题。

    推荐文章