【问题标题】:Bad request when requesting svg.sencha.io with cUrl使用 cUrl 请求 svg.sencha.io 时出现错误请求
【发布时间】:2015-01-14 15:06:57
【问题描述】:

我正在尝试使用 Extjs 的功能将图形转换为图像。我不是直接从 javascript 请求 svg.sencha.io,而是尝试在我的 php 代码中使用 cUrl 调用。由于某些原因,从 php 调用它是我唯一的选择,并且使用我现在使用的 cUrl 代码,我总是收到“错误请求”。所以,我的问题是:这里有没有人处理过这种情况?这是我正在尝试使用的代码:

    $url = 'http://svg.sencha.io';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $file = curl_exec($ch);

    curl_close($ch);

    var_dump($file);

$_POST 包含参数类型、宽度、高度和 svg。它们似乎很好,因为我直接从我的机器上运行 cUrl 并且它有效。所以问题确实出在 cUrl 和 php 上。 var_dump($file);prints string(12) "Bad request."

非常感谢一些帮助,我已经被困了一段时间了。

【问题讨论】:

    标签: javascript php extjs curl svg


    【解决方案1】:

    我找到了解决方案(至少目前看来是可行的)。

    解决方案不是使用 cUrl,而是使用替代方案。因此,我搜索了其中的一些内容并进入了这篇文章:http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/。帖子上的函数一开始对我不起作用,但在阅读了几乎所有的 cmets 之后,我做了一些改动,这是向 svg.sencha.io 发出 POST 请求并正确返回图像的函数:

    function _postRequest($url, $data, $optional_headers = null) {
        $params = array('http' => array(
                        'method' => 'POST',
                        'content' => http_build_query($data)
                   ));
        if ($optional_headers !== null) {
            $params['http']['header'] = $optional_headers;
        }
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            throw new Exception("Problem with $url, $php_errormsg");
        }
        $response = @stream_get_contents($fp);
        if ($response === false) {
            throw new Exception("Problem reading data from $url, $php_errormsg");
        }
        return $response;
    }
    

    根据该帖子评论部分的某人的说法,这种方法比 cUrl 慢(而且过程确实很慢),但显然没有其他选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 2023-03-20
      • 2016-12-08
      • 2016-08-25
      • 2021-10-22
      • 1970-01-01
      相关资源
      最近更新 更多