search
HomeBackend DevelopmentPHP TutorialPHP sends XML data through curl and gets XML data

In the process of learning PHP, you will encounter when PHP sends XML data through curl. This article will explain the related methods.

In php programming, it is often used to transmit data in xml format, such as calling third-party interfaces such as WeChat. Here is a demonstration of php sending xml in curl form and receiving it through the server

1. Send xml data - postXml.php

<?php
 // 首先检测是否支持curlif (!extension_loaded("curl")) {
    trigger_error("对不起,请开启curl功能模块!", E_USER_ERROR);
} 
// 构造xml数据$xmlData = "
<xml>
<AppId>wxf8b4f85f3a794e77</AppId>
<ErrorType>1001</ErrorType>
<Description>错误描述</Description>
<AlarmContent>transaction_id=33534453534</AlarmContent>
<TimeStamp>1393860740</TimeStamp>
<AppSignature>f8164781a303f4d5a944a2dfc68411a8c7e4fbea</AppSignature>
<SignMethod>sha1</SignMethod>
</xml>";
 
 
$url = &#39;http://web.whm.com/getXml.php&#39;; //接收xml数据的文件$ch = curl_init();  // 初始一个curl会话$timeout = 30;  // php运行超时时间,单位秒curl_setopt($ch, CURLOPT_URL, $url);    // 设置urlcurl_setopt($ch, CURLOPT_POST, 1);  // post 请求curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:text/xml; charset=utf-8"));    // 一定要定义content-type为xml,要不然默认是text/html!curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);//post提交的数据包curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); // PHP脚本在成功连接服务器前等待多久,单位秒curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);   // 抓取URL并把它传递给浏览器// 是否报错if(curl_errno($ch))
{    print curl_error($ch);
}
curl_close($ch);    // //关闭cURL资源,并且释放系统资源
 echo $result;


php sends XML data through curl and obtains XML data

PHP sends XML data through curl and gets XML data

2. Receive xml data - getXml.php

<?php
 //接收传送的数据$xml = file_get_contents("php://input"); 
//将xml数据写入文本文件"whm.txt"中$handle =fopen(&#39;whm.txt&#39;,&#39;w&#39;);
 
fwrite($handle,$xml);


php sends XML data through curl and obtains XML data

PHP sends XML data through curl and gets XML data

3. Notes

When constructing xml, be sure to pay attention to the correct format, no spaces, etc.

Be sure to define content-type as xml, otherwise the default is text/html

This article explains how PHP sends XML data through curl and obtains XML data. For more related knowledge, please pay attention to the PHP Chinese website.

Related recommendations:

PHP perfectly generates word documents, and html elements can be added

ThinkPhp caching principle and detailed explanation

Discuz!X/Database DB:: Function operation method

The above is the detailed content of PHP sends XML data through curl and gets XML data. For more information, please follow other related articles on the PHP Chinese website!

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment