如何在PHP中调用HTTP多部分/相关请求? [英] How to call HTTP multipart/related request in PHP?

查看:62
本文介绍了如何在PHP中调用HTTP多部分/相关请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自

From this site, there is a HTTP request I want to call. (not exactly the same for the credentials and content, but the structure are the same) It is used for sending MMS.

POST [XXX] HTTP/1.1
Host: europe.ipx.com
Content-Type: multipart/related; type="text/xml"; boundary="----=_037e2e9a2f4e80683dd5c5ec875fb0ce"
Authorization: Basic [XXX]
Content-Length: 18800
Cache-Control: no-cache 
Pragma: no-cache 
Accept: text/html, image/gif, image/jpg, *; q=.2, */*; q=.2

Connection: keep-alive
------=_037e2e9a2f4e80683dd5c5ec875fb0ce
Content-Type: text/xml; charset=utf-8

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <mm7:TransactionID xmlns:mm7="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-2">[XXX]_20090605211315</mm7:TransactionID>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <mm7:SubmitReq xmlns:mm7="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-2">
            <mm7:MM7Version>6.5.0</mm7:MM7Version>
            <mm7:SenderIdentification>
                <mm7:VASPID>[XXX]</mm7:VASPID>
                <mm7:SenderAddress>
                    <mm7:ShortCode>[XXX]</mm7:ShortCode>
                </mm7:SenderAddress>
            </mm7:SenderIdentification>
            <mm7:Recipients>
                <mm7:To>
                    <mm7:Number>46733[XXX]</mm7:Number>
                </mm7:To>
            </mm7:Recipients>
            <mm7:ServiceCode>[XXX]</mm7:ServiceCode>
            <mm7:DeliveryReport>1</mm7:DeliveryReport>
            <mm7:Subject>MMS Subject</mm7:Subject>
            <mm7:Content href="cid:attachment_1"/>
        </mm7:SubmitReq>

    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
------=_037e2e9a2f4e80683dd5c5ec875fb0ce
Content-Type: application/vnd.wap.multipart.related; boundary="----=_45435153f79e2b654ebf607c4d65e15e"
Content-Id: <attachment_1>

------=_45435153f79e2b654ebf607c4d65e15e
Content-Type: application/smil
Content-ID: <smil_1.smil>

<smil>
<head>
<layout>
<root-layout background-color="#FFFFFF" height="240px" width="160px"/>
<region id="Image" top="0" left="0" height="50%" width="100%" fit="hidden"/>
<region id="Text" top="50%" left="0" height="50%" width="100%" fit="hidden"/>
</layout>
</head>

<body>
<par dur="12000ms">
<img src="pin_image.jpg" region="Image"></img>
<text src="text_1.txt" size="12" region="Text">
<param name="textsize" value="small" />
</text>
</par>
<par dur="8000ms">
<text src="text_2.txt" size="12" region="Text">
<param name="textsize" value="small" />
</text>
</par>
</body>
</smil>

------=_45435153f79e2b654ebf607c4d65e15e
Content-Type: text/plain; charset=utf-8; name=text_1.txt
Content-ID: <text_1.txt>

Hello!
This is the first text-block.
------=_45435153f79e2b654ebf607c4d65e15e
Content-Type: text/plain; charset=utf-8; name=text_2.txt
Content-ID: <text_2.txt>

And this is the second text-block.
------=_45435153f79e2b654ebf607c4d65e15e
Content-Type: image/jpg; name=pin_image.jpg
Content-Transfer-Encoding: base64
Content-ID: <pin_image.jpg>

/* base64-image stripped out from code due to its length */
------=_45435153f79e2b654ebf607c4d65e15e--

------=_037e2e9a2f4e80683dd5c5ec875fb0ce--Array

要发送请求,我在PHP中使用curl函数,但如有必要,我可以使用其他库.

To send the request, I am using curl function in PHP, but I am open to using other library if necessary.

问题是我在通话中看到多个 Content-Type ,据我所知,我无法在中多次设置 Content-Type curl_setopt ?但是,它似乎不是多个HTTP请求,因为 Host 仅出现一次.如何在PHP中发出这样的HTTP请求?

The problem is that I see multiple Content-Type in the call, and as far as I know, I cannot set Content-Type multiple times in curl_setopt? However, it does not seem to be multiple HTTP request as the Host only appears once. How can I make such a HTTP request in PHP?

推荐答案

最简单的方法是使用Guzzle库:

The easiest would be using the Guzzle library:

https://guzzle.readthedocs.io/en/latest/request-options.html#multipart

$client = new \GuzzleHttp\Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://europe.ipx.com',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);

$options = [
    'multipart' => [
        [
            'name'     => 'foo',
            'contents' => 'data',
            'headers'  => ['Content-Type' => 'text/xml']
        ],
        [
            'name'     => 'baz',
            'contents' => fopen('/path/to/file', 'r'),
            'headers'  => ['Content-Type' => 'image/jpeg']
        ],
        [
            'name'     => 'qux',
            'contents' => fopen('/path/to/file', 'r'),
            'filename' => 'custom_filename.txt',
            'headers'  => ['Content-Type' => 'text/plain']
        ],
    ]
]);

正如@hanshenrik在其评论中正确指出的那样,Guzzle客户端无法发送多部分/相关请求,因此按

As @hanshenrik correctly notes in his comment, the Guzzle client cannot send multipart/related request, so some hacking is in order as described in Guzzle multipart/related content type feature request.

    $url = '/post';

    $headers = isset($options['headers']) ? $options['headers'] : [];
    $body = new \GuzzleHttp\Psr7\MultipartStream($options['multipart']);
    $version = isset($options['version']) ? $options['version'] : '1.1';
    $request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $body, $version);

    $modify['set_headers']['Content-Type'] = 'multipart/related; boundary=' . $request->getBody()->getBoundary();

    $request = \GuzzleHttp\Psr7\modify_request($request, $modify);

    $response = $client->send($request);

这篇关于如何在PHP中调用HTTP多部分/相关请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆