dsjswclzh40259075 2012-08-27 08:10
浏览 22
已采纳

PHP的curl没有将数据发布到服务器

I have written a small app that uses curl a while ago and was working. Today I tried to run it and it is not working.

I use curl to post data to another page located on an external server. The Get is working but not the post.

Update 1:

I have narrowed the error down to having an entry called "SOME_KEY" and having the value "'@C:/wamp/www/name.pdf'" in the posts array will cause this problom.

Howcome the @ symbole is causing a problem!!! I am trying to uplaod a file to the server

Solved

Check the answer below.

Original entry

My current curl options

    $curl_options = array(
        CURLOPT_RETURNTRANSFER => true,                 // Return web page      
        CURLOPT_HEADER         => false,                // Don't return headers
        CURLOPT_FOLLOWLOCATION => false,                // Follow redirects
        CURLOPT_ENCODING       => "",                   // Handle all encodings
        CURLOPT_AUTOREFERER    => false,                // Set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,                  // Timeout on connect
        CURLOPT_TIMEOUT        => 120,                  // Timeout on response
        CURLOPT_MAXREDIRS      => 0,                    // Stop after 10 redirects
        CURLOPT_USERAGENT      => "regify connector",   // Who am I? FIREFOX, IE, OPERA, CHROME, SPIDER, SEARCH BOT...etc.
    );

the function that posts the data:

function makePostRequest($url, $fields  ){

        //Create the options
        //global $curl_options;
        $curl_options[CURLOPT_URL       ] = $url;
        $curl_options[CURLOPT_POST      ] = count($fields);
        $curl_options[CURLOPT_POSTFIELDS] = $fields;

        $ch = curl_init();
        curl_setopt_array($ch, $curl_options);

        $content = curl_exec ($ch);
        $err     = curl_errno($ch);

        curl_close($ch);    
        return ($err == 0)?$content:false;
    }

PHP's getallheader is returning

array
  'Host' => string 'localhost' (length=9)
  'Accept' => string '*/*' (length=3)
  'Content-Length' => string '16632433' (length=8)
  'Expect' => string '100-continue' (length=12)
  'Content-Type' => string 'multipart/form-data; boundary=----------------------------93ac4fed717b' (length=70)

CURL's curl_getinfo function is returning:

    array
  'url' => string 'http://localhost/archieve/index.php' (length=35)
  'content_type' => string 'application/json' (length=16)
  'http_code' => int 200
  'header_size' => int 281
  'request_size' => int 263
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0.125
  'namelookup_time' => float 0
  'connect_time' => float 0
  'pretransfer_time' => float 0
  'size_upload' => float 16632433
  'size_download' => float 18
  'speed_download' => float 144
  'speed_upload' => float 133059464
  'download_content_length' => float 18
  'upload_content_length' => float 16632433
  'starttransfer_time' => float 0.015
  'redirect_time' => float 0
  'certinfo' => 
    array
      empty

Can this porblem be cause by the content type? I read on stackoverflow that it recommaded that CURL is smart enough to set this value by itself.

  • 写回答

1条回答 默认 最新

  • dongshaidu2456 2012-08-27 13:32
    关注

    I figured it out. The code was and still working as it should. Curl was not able to post the PDF file, I believe because of permissions, and therefore it was not posting at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度