PHP:在发布multipart / form-data时解析String to array

时间:2013-03-03 11:40:54

标签: php curl

在firefox http标头插件中,我记录了标题和帖子内容。

标题低于

Content-Type: multipart/form-data; boundary=---------------------------2794991859960663061251376577

,发布内容低于

-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="action"\r\n
\r\n
post\r\n 
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="_wp_http_referer"\r\n
\r\n
/submit/\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="cat"\r\n
\r\n
62\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="post_title"\r\n
\r\n

我转换成如下所示的卷曲。

$fields = '-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="action"\r\n
\r\n
post\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="_wpnonce"\r\n
\r\n
c9aa342328\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="_wp_http_referer"\r\n
\r\n
/submit/\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="cat"\r\n
\r\n
62\r\n
-----------------------------2794991859960663061251376577\r\n
Content-Disposition: form-data; name="post_title"\r\n
\r\n';
    curl_setopt($ch,CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(

        'Content-Type: multipart/form-data; boundary=---------------------------2794991859960663061251376577',

    ));

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch,CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch,CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch); 
    $last = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return array($result,$last,$info);
    curl_close($ch);

但是当我发现http标头时,它只发送文字/ html

HTTP/1.1 200 OK Transfer-Encoding: chunked Date: Sun, 03 Mar 2013 11:38:01 GMT Server: LiteSpeed Connection: close X-Powered-By: PHP/5.2.17 X-Pingback: http://domain.com/xmlrpc.php Content-Type: text/html; charset=UTF-8 Expires: Wed, 11 Jan 1984 05:00:00 GMT Cache-Control: no-cache, must-revalidate, max-age=0 Pragma: no-cache Vary: User-Agent 

我检查过php.net,它说If value is an array, the Content-Type header will be set to multipart/form-data.

现在如何将$ fields转换为数组?这样数据将以multipart / form-data格式发送。

0 个答案:

没有答案