2015-01-14 15:06
浏览 91


I'm trying to use Extjs's feature to convert graphics into images. I'm not requesting directly from the javascript, instead, I'm trying to call using cUrl in my php code. For some reasons, calling it from the php is my only option and with the cUrl code that I'm using right now, I always get "Bad request". So, my question is: Have anyone around here dealt with this situation before? Here is the code I'm trying to work with:

    $url = '';

    $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);



The $_POST contains the parameters type, width, height and svg. They seem to be fine, because I runned cUrl directly from my machine and it worked. So the problem really is with cUrl and php. The var_dump($file);prints string(12) "Bad request."

I would really appreciate some help, I've been stuck in this for awhile now.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwen5741
    douwen5741 2015-01-14 16:20

    I found the solution (at least for now it seems to be working).

    The solution was not use cUrl and instead use an alternative. So, I searched for some of that and I came into this post: The function on the post didn't work for me at first, but after reading almost all of the comments, I made a little change and this is the function that make a POST request to and returns the image correctly:

    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;

    According to someone on the comment section of that post, this method is slower than cUrl (and the process is slow indeed), but apparently there isn't other options.

    点赞 评论