search
Homephp教程php手册将XML文件http post到某个地址

最近做了个小任务,短信、彩信的群发功能。合作方提供了一个接口,我们只要把需要发送的内容拼成一个XML,再http post到那个地址就行。这里用到了curl这个库,简单记录下用法。

下面是短信群发的小程序,先从一个上传的电话号码文本获取号码,然后获取内容发送即可。

<?php

if($_FILES['phone_num']['error']>0) {
	echo 'Problem:';
  	switch ($_FILES['phone_num']['error'])
  	{
		//1表示文件超过php配置里的大小限制
  		case 1: echo 'File exceeded max in phi.ini!';break;
		//2表示超过最大限制
   		case 2: echo 'File exceeded max_file_size';break;
		//3表示部分上传
   		case 3: echo 'File only partially uploaded';break;
		//4表示没有上传
   		case 4: echo 'No file upload'; break;
  	}
	exit;
}

// 如果文件类型非纯文本,输出提示 
if ($_FILES['phone_num']['type'] != 'text/plain') {
	echo 'Problem:file is not plain text';
	exit;
}

// 转移文件路径,转移失败,输出错误
$dir = dirname(__file__).'/upload/';
$filename = $_FILES['phone_num']['name'];
$savepath = "$dir/$filename";
 
if (is_uploaded_file($_FILES['phone_num']['tmp_name'])) {
	$state = move_uploaded_file($_FILES['phone_num']['tmp_name'], $savepath);
	//如果上传成功,预览
	if($state)
	{
   		//echo "<img src='$filename' alt='$filename' /> ";
	}
	/**
	if (!move_uploaded_file($_FILES['phone_num']['tmp_name'], $savepath)) {
		echo 'Problem could not move file to destination directory';
		exit;
	}
	*/
}
else 
{
	echo 'Problem :possible file upload attack file:';
	echo $_FILES['phone_num']['name'];
	exit;
}

$pn = file_get_contents($savepath); 
$content = $_POST['content'];

$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TaskDataTransfer4EReq xmlns="http://www.aspirehld.com/iecp/TaskDataTransfer4EReq">
<eid>3100</eid>
<username>lwxkk</username>
<password>1234567</password>
<src>106581036177</src>
<destmsisdn>'.$pn.'</destmsisdn>
<content type="sms">
	<title>'.$content.'</title>
</content>
</TaskDataTransfer4EReq>';

$url = 'http://www.bkjia.com/service/taskSubmit';//接收XML地址

$header = "Content-type: text/xml";//定义content-type为xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
    print curl_error($ch);
}
curl_close($ch); //关闭curl链接
echo $response;//显示返回信息
?>

彩信群发则稍稍麻烦些,需要按要求把文本、图片、mms.smil等文件按规则命名并打包好,但是发送的原理还是一样的。

<?php
// 对彩信包的处理繁琐但是简单,这里省略
$encoded = chunk_split(base64_encode($file_content));

$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TaskDataTransfer4EReq xmlns="http://www.aspirehld.com/iecp/TaskDataTransfer4EReq">
<eid>310</eid>
<username>lwxk</username>
<password>123456</password>
<src>106581036177</src>
<destmsisdn>'.$pn.'</destmsisdn>
<content type="mms">
<title>'.$title.'</title>
<mmsfile>'.$encoded.'</mmsfile>
</content>
</TaskDataTransfer4EReq>';

$url = 'http://www.bkjia.com/service/taskSubmit';//接收XML地址

$header = "Content-type: text/xml";//定义content-type为xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
    print curl_error($ch);
}
curl_close($ch); //关闭curl链接
echo $response;//显示返回信息
?>

其实很简单,就是将内容拼成一个XML字符串,按要求base_64编好码,再post到该地址就行。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools