Home Backend Development PHP Tutorial HTTP post XML file to a certain address_PHP tutorial

HTTP post XML file to a certain address_PHP tutorial

Jul 13, 2016 am 10:33 AM
curl xml

Recently I did a small task, the group sending function of SMS and MMS. The partner provides an interface. We only need to put the content to be sent into an XML, and then http post to that address. The curl library is used here, and the usage is simply recorded.

The following is a small program for mass text messaging. First, get the number from an uploaded phone number text, and then get the content and send it.

<?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;//显示返回信息
?>
Copy after login

Sending multimedia messages in bulk is a little more troublesome. You need to name and package text, pictures, mms.smil and other files according to the requirements, but the principle of sending is still the same.

<?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;//显示返回信息
?>
Copy after login

In fact, it is very simple, just put the content into an XML string, encode it in base_64 as required, and then post it to the address.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752471.htmlTechArticleRecently I did a small task, the group sending function of SMS and MMS. The partner provides an interface. We only need to put the content to be sent into an XML, and then http post to that address. ...
Statement of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles