dqgo99177 2019-03-30 19:52
浏览 263
已采纳

如何正确格式化此字符串以在URL中使用

I'm trying to create an image of a chart I'm using so that I can embed it into a pdf. I'm using chart.js to create the chart - although I'm using https://quickchart.io/ to create the chart by passing the chart information to the quickchart url.

I'm then trying to add it to a pdf by using tcpdf.

The string which I create from some arrays is:

$genderGraph = "https://quickchart.io/chart?c={type: 'doughnut',data:{labels:" . json_encode($genderchartjs['label']) . ", datasets: [{data:" . json_encode($genderchartjs['data']) . ",backgroundColor:" . json_encode($chartcolors) . "}]}}";

And if I echo what $genderGraph is following the above its:

https://quickchart.io/chart?c={type: 'doughnut',data:{labels:["Male","Female","Unknown"], datasets: [{data:[16,34,17],backgroundColor:["rgba(255, 99, 132, 1)","rgba(54, 162, 235, 1)","rgba(255, 206, 86, 1)","rgba(75, 192, 192, 1)","rgba(153, 102, 255, 1)","rgba(255, 159, 64, 1)","rgba(0, 0, 0, 1)"]}]}}

Which if you stick in a browser address bar will show you the correct image of the chart, exactly as I want it.

The problem is when I try to add the image to the pdf using file_get_contents()

$img = file_get_contents($genderGraph);
$pdf->Image('@' . $img);

I get the following warning:

Warning (2): file_get_contents(https://quickchart.io/chart?c={type: 'doughnut',data:{labels:["Male","Female","Unknown"], datasets: [{data:[16,34,17],backgroundColor:["rgba(255, 99, 132, 1)","rgba(54, 162, 235, 1)","rgba(255, 206, 86, 1)","rgba(75, 192, 192, 1)","rgba(153, 102, 255, 1)","rgba(255, 159, 64, 1)","rgba(0, 0, 0, 1)"]}]}}): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

It appears to be a problem with the formatting of the URL, what do I need to do to fix this?

  • 写回答

1条回答 默认 最新

  • dsvjw20866 2019-03-30 20:04
    关注

    Lets open the doc for file_get_contents() here

    Note:

    If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

    You should encode your query parameter using urlencode or better http_build_query

    Example:

    <?php
    $url = 'https://quickchart.io/chart';
    
    // replace with your string
    $c = "{type: 'doughnut',data:{labels:[\"Male\",\"Female\",\"Unknown\"], datasets: [{data:[16,34,17],backgroundColor:[\"rgba(255, 99, 132, 1)\",\"rgba(54, 162, 235, 1)\",\"rgba(255, 206, 86, 1)\",\"rgba(75, 192, 192, 1)\",\"rgba(153, 102, 255, 1)\",\"rgba(255, 159, 64, 1)\",\"rgba(0, 0, 0, 1)\"]}]}}";
    
    $url = $url . '?' . http_build_query([
        'c' => $c
    ]);
    
    $image = file_get_contents($url);
    // pdf
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题