Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有文件附件的PHP Curl post;自定义内容类型标题_Php_Curl_Multipartform Data - Fatal编程技术网

带有文件附件的PHP Curl post;自定义内容类型标题

带有文件附件的PHP Curl post;自定义内容类型标题,php,curl,multipartform-data,Php,Curl,Multipartform Data,我必须将一个文件和几个post参数一起发布到服务器。我从这个服务器管理员那里收到的文档显示了post请求应该是什么样子的示例(*注意post“multipart/x-api-remote-integration”的自定义内容类型): 下面是php代码: <?php function post(){ $base_api_url = "https://hostserver.com/gateway/remote_send"; $filename = realpath("/hom

我必须将一个文件和几个post参数一起发布到服务器。我从这个服务器管理员那里收到的文档显示了post请求应该是什么样子的示例(*注意post“multipart/x-api-remote-integration”的自定义内容类型):

下面是php代码:

<?php
function post(){
    $base_api_url = "https://hostserver.com/gateway/remote_send";

    $filename = realpath("/home/username/tests/test1234qwerty.csv");

    $payload['profile_name'] = "username";
    $payload['profile_pw'] = 'password1234';
    $payload['attached_type'] = 'action_1';
    $payload['filename'] = "@" . $filename . ";type=text/csv;";

    $curl_options = array(
        CURLOPT_URL => $base_api_url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query( $payload ),
        CURLOPT_HTTP_VERSION  => 1.0,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_HEADER => false,
    );

    $curl = curl_init();
    curl_setopt_array( $curl, $curl_options );
    $result = curl_exec( $curl );
    curl_close ($curl);
    echo $result;
  }

post();

告诉cURL上载文件时,
内容类型
标题将自动设置为
多部分/表单数据
。对于文档中的请求,您必须手动上载文件


尝试从根目录运行代码,并使用
$filename=realpath(“tests/test1234qwerty.csv”)看看这是否有帮助。我以前在文件夹名称前面使用
/
以及使用这种路径时遇到过类似的问题。感谢您的快速回复和建议。我刚刚试过,不幸的是,我仍然看到关于文件附件的相同响应。不客气。对不起,我怀疑我能帮上什么忙。祝你一切顺利,干杯谢谢你,先生!我对您感激不尽。@mishanon是的,两次–一次是主表单数据,另一次是附加的CSV文件。这两个内容类型标题都出现在POST数据中–主内容类型标题设置为
multipart/x-api-remote-integration
<?php
function post(){
    $base_api_url = "https://hostserver.com/gateway/remote_send";

    $filename = realpath("/home/username/tests/test1234qwerty.csv");

    $payload['profile_name'] = "username";
    $payload['profile_pw'] = 'password1234';
    $payload['attached_type'] = 'action_1';
    $payload['filename'] = "@" . $filename . ";type=text/csv;";

    $curl_options = array(
        CURLOPT_URL => $base_api_url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => http_build_query( $payload ),
        CURLOPT_HTTP_VERSION  => 1.0,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_HEADER => false,
    );

    $curl = curl_init();
    curl_setopt_array( $curl, $curl_options );
    $result = curl_exec( $curl );
    curl_close ($curl);
    echo $result;
  }

post();
* About to connect() to hostserver.com port 443 (#0)
*   Trying x.x.x.x... * connected
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using DHE-RSA-AES256-SHA
*   ******** REDACTED ************
*   SSL certificate verify ok.
> POST /gateway/remote_send HTTP/1.0
Host: hostserver.com
Accept: */*
Content-Length: 110
Content-Type: application/x-api-remote-integration

* upload completely sent off: 110out of 110 bytes
< HTTP/1.1 200 OK
< Date: Mon, 19 Aug 2013 16:00:38 GMT
< Server: Apache/2.2.15 (Red Hat)
< Pragma: no-cache
< CacheControl: no-cache
< Cache-Control: no-cache
< Expires: -1
< Connection: close
< Content-Type: multipart/x-api-remote-integration; boundary=remote_send-52124126
<
* Closing connection #0
HTTP/1.1 200 OK
Date: Mon, 19 Aug 2013 16:00:38 GMT
Server: Apache/2.2.15 (Red Hat)
Pragma: no-cache
CacheControl: no-cache
Cache-Control: no-cache
Expires: -1
Connection: close
Content-Type: multipart/x-vcg-remote-api; boundary=remote_send-52124126

--remote_send-52124126
Content-Type: application/x-www-form-urlencoded

result=invalid_filename
--remote_send-52124126--