【发布时间】:2012-10-10 08:16:01
【问题描述】:
可能重复:
What’s wrong with my PHP curl request, please help .. I’m not getting any data back
我将 XML 数据发布到应该向我发送(回显)结果的 URL,而不是将它们发送回给我,这是我在运行 PHP 后得到的 .. 我的 curl 代码在此下方结果...我怀疑下面的 CURL 代码可能有问题,我如何才能从发布到 URL(mysgproc.php)的 XML 数据中实际获取结果,我也发布了 url 代码?非常感谢
HTTP/1.1 200 OK
Date: Fri, 19 Oct 2012 16:30:26 GMT
Server: Apache/2.2.9 (Fedora)
Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/
Content-Length: 238
Connection: close
Content-Type: text/html
<?xml version="1.0" encoding="UTF-8"?><iniResponseMsg deliveryTimeStamp="19/10/2012 16:30:26 GMT" messageID="9d085e45-9e9b-11da-befd-0014221b359d" correlationID="69c8277d-77af-4fd9-8cc0-7c30d2963bee"> <state>fail</state></iniResponseMsg>
下面是我的代码...
<?php
$url ="http://127.0.0.1/sens/msgproc.php";
$xml_data = file_get_contents("/usr/local/www/data/message_test.xml");
#$header ="POST HTTP/1.0 \r\n";
#$header .="Content-type: text/xml \r\n";
#$header .="Content-length: ".strlen($xml_data)." \r\n";
#$header .="Content-transfer-encoding: text\r\n";
#$header .="Connection: close \r\n\r\n";
#$header .= $xml_data;
$headers = array('Content-Type: text/xml','Content-Transfer-Encoding: text','Connection: close');
$ch = curl_init();
curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xml_data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
#curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
$data = curl_exec($ch); // if the post is successful , the server will return some data.
#$info = curl_getinfo($ch);
#
#if(!curl_errno($ch))
# echo 'It took '.$info['total_time'].'seconds to send a request to'.$info['url'];
#
# else
#
curl_close($ch);
echo $data;
?>
这里是 msgproc.php 代码
<?php
//$pass='go';
/*******************************************************************************
* COMPONENT NAME:
* msgproc.php
*
* DESCRIPTION:
* This script accepts the various XML documents from G*.
* The document is passed to the correct routine
* in msgproc.php based on what it is.
*
*******************************************************************************/
require('sensdb.php');
require('dbutil.php');
require('msgproc_util.php');
// first this is to pipe all output to the buffer for loggin later in the file
// echo and print statements will get logged instead of outputted
ob_start();
//$raw_post_data = $HTTP_RAW_POST_DATA;
$raw_post_data = file_get_contents("php://input");
$time_log = date("d/m/Y G:i:s") . ' - aeroastro sensor sys time';
/* CONNECT TO THE DATABASE */
sensdb_connect();
$script_time = date("M-d-Y H:i:s");
// logging variables
$type='';
$byte_count=0;
$message_id='';
$error='';
$error_type='';
$source = $_SERVER['REMOTE_ADDR'];
/*******************************************************************************
* COMPONENT NAME:
* main process
*
* DESCRIPTION:
*
*******************************************************************************/
/****** formerly function test_read_xml_post() ***********************/
//$fp = fopen("/usr/local/www/data/sensor/sens/message_test.xml", "r");
//$fp = fopen("php://input", "r");
echo "string length:" . strlen($raw_post_data) . ' ';
// reject all data not from Globalstar of development
//if ( $source == "207.88.248.142" ) {
$data = trim($raw_post_data);
//} else {
// $data = NULL;
//}
$byte_count += strlen($data);
//fclose($fp);
// test of php5 parsing xml .... much easier
if ( $xml = simplexml_load_string($data) ) {
// innocent until proven guilty
$status = true;
// this will help us find the root element to determine message type
$dom = dom_import_simplexml($xml);
$xml_root = $dom->tagName;
// process stumessages here
if ( $xml_root == 'stuMessages' ) {
$type = 'STxMessage';
// what is the message id
$xml_messageID = $xml->xpath('/stuMessages/@messageID');
foreach ( $xml_messageID as $id ) {
$message_id = $id;
}
// what is the packet time
$packetTimes = $xml->xpath('/stuMessages/@timeStamp');
foreach ( $packetTimes as $_time ) {
$packetTime = $_time;
}
$time_log .= "\n". $packetTime . ' - globalstar packet time';
// how many messages
$message_count = count($xml->xpath('/stuMessages/stuMessage'));
if ( $message_count >= 1 ) {
// fomat the sql string
$sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
if ( mysql_query($sql) ) {
// get the insert id
// be careful not to change the sql above which might break our ability here to get the last insert id
$incoming_id = mysql_insert_id();
echo $script_time . " - stu_message packet $message_id logged: insert_id:$incoming_id";
} else {
echo $script_time . " - stu_message failed logging";
}
// mysql_query("LOCK TABLES STxMessage WRITE");
// mysql_query("SET AUTOCOMMIT = 0");
mysql_query("BEGIN");
foreach( $xml->stuMessage as $stuMessage ) {
$unixtime = $stuMessage->unixTime;
$gps = $stuMessage->gps;
$payload = $stuMessage->payload;
// convert to an ESN int
$esn = $stuMessage->esn;
$esn = esn_to_num($esn);
// test the message time against system time
// JL 2007
$time_log .= "\n" . gmdate("d/m/Y H:i:s", "$unixtime") . " - packet stu message time";
// JL 2006 Apr
// log message data larger than 255 chars for TSM account a la Tony B
// these messages don't fit in our DB
if ( strlen($payload) > 255 ) {
// log any output
$large_message_log = '/usr/local/www/logs/large_message_log';
$sql = 'INSERT INTO STxLargeMessage ( ESN,GPS,Payload,UnixTime,IncomingID) values ("'.$esn.'","'.$gps.'","'.$payload.'","'.$unixtime.'","'.$incoming_id.'")';
// Perform Insert of large message
$result = @mysql_query($sql);
// Let's make sure the file exists and is writable first.
if ( is_writable($large_message_log) ) {
// The file pointer is at the bottom of the file hence
$handle = @fopen($large_message_log, 'a');
// Write $response to our opened file.
@fwrite($handle, "\n".$data);
@fclose($handle);
}
}
// do we have an ESN for this message
// $select_esn = 'select ESN from STx where ESN = "'.$esn.'"';
// $result = mysql_query($select_esn);
// $result_count = mysql_num_rows($result);
$sql = 'INSERT INTO STxMessage( ESN,GPS,Payload,UnixTime,IncomingID) values ("'.$esn.'","'.$gps.'","'.$payload.'","'.$unixtime.'","'.$incoming_id.'")';
// Perform Insert
$result = mysql_query($sql);
$GMT_unixtime = gmdate("Y-m-d H:i:s", "$unixtime");
$update = 'update STx set MessageCount = MessageCount + 1, LastMessageTime = "' . $GMT_unixtime . '" where ESN = "' . $esn . '"';
// increment the message count by 1
mysql_query($update);
unset($update);
// Check db result
if ( !$result ) {
$status = false;
// log the error
echo 'Invalid query: ' . mysql_error() . "\n";
echo $message .= 'Whole query: ' . $query;
}
unset($result);
unset($unixtime);
unset($gps);
unset($payload);
unset($esn);
}
if ( $status ) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
// mysql_query("UNLOCK TABLES");
// mysql_query("SET AUTOCOMMIT = 1");
} else { // no data but valid xml, return pass, probably G* "pining" aeroastro for life
$error = "no data in xml packet id:" . $message_id . "\n";
echo $error;
}
// make our response xml for globalstar
$response = make_globalstar_response_xml('stuResponseMsg', $message_id, $status);
} elseif ( $xml_root == 'prvmsgs' ) { // process prvmsgs messages
/*******************************************************************************
* COMPONENT NAME:
* PRVMSGS
*
* DESCRIPTION:
* These routines accept the PRVMSGS sent by Globalstar in response to the
* INIMSGS that were sent. This routine stores the PRVMSGS into the database,
* and also sends a respose message back to Globalstar.
*
*******************************************************************************/
$type = 'PrvMessage';
// what is the message id
$xml_messageID = $xml->xpath('/prvmsgs/@prvMessageID');
foreach ( $xml_messageID as $id ) {
$message_id = $id;
}
// how many messages
$message_count = count($xml->xpath('/prvmsgs/prvmsg'));
if ( $message_count >= 1 ) {
// fomat the sql string for logging
$sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
if ( mysql_query($sql) ) {
// get the insert id
$incoming_id = mysql_insert_id();
echo $script_time . " - PrvMessage packet $message_id logged";
} else {
echo $script_time . " - PrvMessage failed logging";
}
mysql_query("BEGIN");
// loop through the messages
foreach( $xml->prvmsg as $prvmsg ) {
$esn = esn_to_num($prvmsg->esn);
$provid = $prvmsg->provID;
$tstart = $prvmsg->tStart;
$tend = $prvmsg->tEnd;
$txretryminsec = $prvmsg->txRetryMinSec;
$txretrymaxsec = $prvmsg->txRetryMaxSec;
$txretries = $prvmsg->txRetries;
$rfchannel = $prvmsg->rfChannel;
$sql = 'REPLACE INTO PrvMessage ( ESN, ProvID, TStart, TEnd, TxRetryMinSec, TxRetryMaxSec, TxRetries, RFChannel, IncomingID) values ("'.$esn.'","'.$provid.'","'.$tstart.'","'.$tend.'","'.$txretryminsec.'","'.$txretrymaxsec.'","'.$txretries.'","'.$rfchannel.'","'.$incoming_id.'")';
// Perform Insert
$result = mysql_query($sql);
// insert status "change" record
$insert_sql = 'insert INTO STxStatusChangeHistory (StatusChangeID, ESN, Status, StatusChangeTimestamp) values ("","'.$esn.'","Provisioned",NOW())';
// Perform Insert
$insert = mysql_query($insert_sql);
// Check result
if (! $result) {
$status = false;
// log the error
echo 'Invalid provisioning: ' . mysql_error() . "\n";
echo 'Whole query: ' . $sql;
} else {
$sql = 'UPDATE STx SET Status="Provisioned" WHERE ESN ="'.$esn.'"';
$result = mysql_query($sql);
// Check result
if (! $result) {
$status = false;
// log the error
echo 'Invalid STx update during provisioning: ' . mysql_error() . "\n";
echo 'Whole query: ' . $sql;
}
}
unset($result);
unset($esn);
unset($provid);
unset($tstart);
unset($tend);
unset($txretryminsec);
unset($txretrymaxsec);
unset($txretries);
unset($rfchannel);
}
if ( $status ) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
msglog_update($message_id, "PrvMessage INSERT ERROR");
}
}
// make our response xml for globalstar
$response = make_globalstar_response_xml('prvResponseMsg', $message_id, $status);
} elseif ( $xml_root == 'instmsgs' ) { // process instmsgs messages
/*******************************************************************************
* COMPONENT NAME:
* INSTMSGS
*
* DESCRIPTION:
* After the VAR gets the PRVMSGS and installs the RTU, it will send the
* INSTMSG back here to indicate where the unit was installed.
*
*******************************************************************************/
$type = 'InstMessage';
// what is the message id
$xml_messageID = $xml->xpath('/instmsgs/@instMessageID');
foreach ( $xml_messageID as $id ) {
$message_id = $id;
}
// how many messages
$message_count = count($xml->xpath('/instmsgs/instmsg'));
if ( $message_count >= 1 ) {
// fomat the sql string for logging
$sql = get_msglog_sql($type, $source, $byte_count, $message_id, $error );
if ( mysql_query($sql) ) {
// get the insert id
$incoming_id = mysql_insert_id();
echo $script_time . " - InstMessage packet $message_id logged";
} else {
echo $script_time . " - InstMessage failed logging";
}
mysql_query("BEGIN");
// loop through the messages
foreach( $xml->instmsg as $instmsg ) {
$esn = esn_to_num($instmsg->esn);
$provid = $instmsg->provID;
$rfchannel = $instmsg->rfChannel;
$tactual = $instmsg->tActual;
$deltat = $instmsg->deltaT;
$latitude = $instmsg->latitude;
$longitude = $instmsg->longitude;
$txretries = $instmsg->txRetries;
$txoffset = $instmsg->txOffset;
$txretryminsec = $instmsg->txRetryMinSec;
$txretrymaxsec = $instmsg->txRetryMaxSec;
$powerlevel = $instmsg->powerLevel;
$sql = 'REPLACE INTO InstMessage ( ESN
,ProvID
,IncomingID
,RFChannel
,TActual
,DeltaT
,Latitude
,Longitude
,TxRetries
,TxOffset
,TxRetryMinSec
,TxRetryMaxSec
,PowerLevel ) values ("'.$esn.'"
,"'.$provid.'"
,"'.$incoming_id.'"
,"'.$rfchannel.'"
,"'.$tactual.'"
,"'.$deltat.'"
,"'.$latitude.'"
,"'.$longitude.'"
,"'.$txretries.'"
,"'.$txoffset.'"
,"'.$txretryminsec.'"
,"'.$txretrymaxsec.'"
,"'.$powerlevel.'")';
// Perform Insert
$result = mysql_query($sql);
// Check result
if (! $result) {
$status = false;
// log the error
echo 'Invalid InstMessage sql: ' . mysql_error() . "\n";
echo 'Whole query: ' . $sql;
} else {
$sql = 'UPDATE STx SET Status="Installed" WHERE ESN ="'.$esn.'"';
$result = mysql_query($sql);
// Check result
if (! $result) {
$status = false;
// log the error
echo 'Invalid STx update during Install: ' . mysql_error() . "\n";
echo 'Whole query: ' . $sql;
}
}
unset($result);
unset($esn);
unset($provid);
unset($tstart);
unset($tend);
unset($txretryminsec);
unset($txretrymaxsec);
unset($txretries);
unset($rfchannel);
unset($txoffset);
unset($txretryminsec);
unset($txretrymaxsec);
unset($powerlevel);
}
if ( $status ) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
msglog_update($message_id, "InstMessage INSERT ERROR");
}
}
// make our response xml for globalstar
$response = make_globalstar_response_xml('instResponseMsg', $message_id, $status);
} elseif ( $xml_root == 'inimsgs' ) { // process inimsgs messages
/*******************************************************************************
* COMPONENT NAME:
* INIMSGS
*
* DESCRIPTION:
* The 'INIMSGS' are the messages sent to begin the provisioning process. The
* message contains a range of ESN numbers to be provisiond. The 'INIMSGS'
* are forwarded to Globalstar. Then, the 'get_prvmsgs' function is called
* to poll the database for the response 'prvmsgs' that Globalstar will send.
*
*******************************************************************************/
$type = 'inimsgs';
// what is the message id
$xml_messageID = $xml->xpath('/inimsgs/@iniMessageID');
foreach ( $xml_messageID as $id ) {
$message_id = $id;
}
// how many messages
$message_count = count($xml->xpath('/inimsgs/inimsg'));
if ( $message_count >= 1 ) {
/* DONT FORGET - have a trigger in the database to set a default route for NASCORP */
$sql = "INSERT INTO STx (ESN, RouteID, GroupID, SubGroupID, STxModel, STxModelGenType, STxName, Note, Status, FilterProfile) VALUES ";
$sep = "";
/* FOR EACH OF THE MESSAGES IN THE PACKET.. .*/
foreach ( $xml->inimsg as $inimsg ) {
$groupid = $inimsg->groupID;
$subgroupid = strlen($inimsg->subGroupID) ? $inimsg->subGroupID : 'NULL';
/* FIRST CHECK THAT THE REQUESTED RANGE IS AVAILABLE */
$esnStart = $inimsg->esnStart;
$esnEnd = $inimsg->esnEnd;
$number = check_esn_range($esnStart, $esnEnd, $inuse);
$isreal = ($inimsg->isReal == 'Yes') ? true : false;
$route_id = get_route_id($groupid, $inimsg->routeAddress);
if ( $number > 0 ) {
/* SPLIT THE ESN NUMBER INTO MANUFACTURER AND SERIAL NUMBER */
$esnS = explode('-', $esnStart);
$esnE = explode('-', $esnEnd);
/* FOR EACH ESN IN THE SPECIFIED RANGE... */
for ( $mfg=(int) $esnS[0]; $mfg <= (int) $esnE[0]; $mfg++ ) {
for ( $ser=(int) $esnS[1]; $ser <= (int) $esnE[1]; $ser++ ) {
$sxtmodel = $inimsg->stxModel;
$sxtmodelgentype='1';
switch ($sxtmodel) {
case "101-1":
$sxtmodel = '101';
$sxtmodelgentype = "1";
break;
case "101-2":
$sxtmodel = '101';
$sxtmodelgentype = "2";
break;
case "101-3":
$sxtmodel = '101';
$sxtmodelgentype = "3";
break;
}
$esn = (($mfg << 23) | $ser);
$sql .= $sep;
$sql .= sprintf("(%d ,%d ,%d ,%s ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' ,'%s' )",
$esn
,$route_id
,$groupid
,$subgroupid
,mysql_escape_string($sxtmodel)
,$sxtmodelgentype
,mysql_escape_string($inimsg->stxName)
,mysql_escape_string($inimsg->note)
,$isreal ? 'Initiated' : 'Fake'
,$inimsg->filterProfile);
$sep = ",";
}
}
$groupid = $inimsg->groupID;
$subgroupid = $inimsg->subGroupID;
$esnstart = esn_to_num($inimsg->esnStart);
$esnend = esn_to_num($inimsg->esnEnd);
$stxmodel = $inimsg->stxModel;
$stxname = $inimsg->stxName;
// new filter setting
$filterprofile = $inimsg->filterProfile;
$routeaddress = $inimsg->routeAddress;
// $mobility = $inimsg->mobility;
// $latdefault = $inimsg->latDefault;
// $londefault = $inimsg->lonDefault;
$stxspacing = $inimsg->txSpacing;
// $txfrequency = $inimsg->txFrequency;
// $trequestedstart = $inimsg->tRequestedStart;
// $trequestedend = $inimsg->tRequestedEnd;
$note = $inimsg->note;
$isreal = $inimsg->isReal;
$insert_inimessage = 'INSERT INTO IniMessage (GroupID
,SubGroupID
,ESNStart
,ESNEnd
,STxModel
,STxName
,FilterProfile
,TxSpacing
,RouteAddress
,Note
,IsReal) VALUES ("'.$groupid.'"
,"'.$subgroupid.'"
,"'.$esnstart.'"
,"'.$esnend.'"
,"'.$stxmodel.'"
,"'.$stxname.'"
,"'.$filterprofile.'"
,"'.$stxspacing.'"
,"'.$routeaddress.'"
,"'.$note.'"
,"'.$isreal.'")';
mysql_query("BEGIN");
if ( mysql_query($insert_inimessage) ) {
// not used for anything
$inserted = true;
} else {
$status=false;
}
unset($groupid);
unset($subgroupid);
unset($esnstart);
unset($esnend);
unset($stxmodel);
unset($stxname);
unset($filterprofile);
unset($routeaddress);
unset($mobility);
unset($stxspacing);
unset($note);
unset($isreal);
} else {
$status = false;
echo $script_time . " invalid ESN range\n".$raw_post_data;
}
}
/* IF THE CONTENTS OF THE MESSAGE LOOKED OK, THEN COMMIT
IT TO THE DATABASE... */
/* CREATE THE STx TABLE ENTRIES */
if ( $status ) {
if ( $status = mysql_query($sql) ) {
mysql_query("COMMIT");
} else {
$doc->errors = mysql_error();
mysql_query("ROLLBACK");
}
} else {
$doc->errors = mysql_error();
mysql_query("ROLLBACK");
}
} else {
$status = false;
echo $script_time . " no messages in xml \n".$raw_post_data;
}
// make our response xml for globalstar
$response = make_globalstar_response_xml('iniResponseMsg', $message_id, $status);
}
} else {
// failed to load xml so lets fail
$status=false;
$response = make_globalstar_response_xml('xml_error', 0, $status);
echo $script_time . " - xml error\n" . $raw_post_data;
}
// log any output
$msgproc_log = '/usr/local/www/logs/msgproc_log';
// collect any trash here for logging
$buffer_string = ob_get_contents()."\n";
// Let's make sure the file exists and is writable first.
if ( is_writable($msgproc_log) ) {
// The file pointer is at the bottom of the file hence
$handle = @fopen($msgproc_log, 'a');
// Write $response to our opened file.
@fwrite($handle, $buffer_string);
@fclose($handle);
}
//empty buffer to logs so that globalstar gets response only
ob_end_clean();
mysql_close();
// send the output for Globalstar to see
echo $response;
?>
【问题讨论】:
-
您要继续发布多少次?这是您的第三次尝试。