柳暗花明笔记

    php 使用curl post数据的时候要注意的地方,抗人的CURLOPT_POSTFIELDS

    所属分类:[ php&web ] 猫哲学家 发表于:2015-11-25 15:53 浏览:546

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    // curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT,180);
    $header = array(
        "token:{$token}",
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    $response = curl_exec($ch);
    curl_close($ch);

    如果没有上传文件的情况下务必要http_build_query,

    不然数据量大或有中文就极可能POST不过去的,我曾经深受其害啊。。。

    官方手册:

    The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like ‘para1=val1&para2=val2&…’ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

    标签: php 
    上一编文章 只有经历过才会懂 php file_get_contents curl fsockopen 浅谈使用场景 下一编文章 使用rsync断点续传从远程服务器上下载文件
    最近文章
    发表评论