从this site开始,我要调用一个HTTP请求。 (凭证和内容不完全相同,但结构相同)它用于发送彩信。
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函数,但如果需要,我愿意使用其他库。
问题是我在通话中看到多个Content-Type
,据我所知,我无法在Content-Type
多次设置curl_setopt
?但是,它似乎不是多个HTTP请求,因为Host
只出现一次。如何在PHP中发出这样的HTTP请求?
答案 0 :(得分:0)
最简单的方法是使用Guzzle库:
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客户端无法发送多部分/相关请求,因此正如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);