1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Sending and Recieving XML via cURL

Discussion in 'PHP' started by cesarcesar, Aug 4, 2007.

  1. #1
    I'm new to sending, receiving, and reading XML. I'm trying to send and receive XML data via cURL. I am using curl because it was so easy with just arrays. I am using the following code to generate the XML and the PHP script that is presently not working. It would be great if some great code god can explain whats wrong to me.

    build_xml.php
    
    <?php
    '<?xml version="1.0"?>'
    $xml = "<member><name>name</name></member>";
    echo $xml;
    ?>
    
    Code (markup):
    Array Method - Works great with arrays. I tried with XML as $data, and it no work. Getting it working this way is preferred.
    
    <?php
    exec("/usr/bin/curl -m 120 -d \"$xml\" http://www.example.com/index.php -L", $response);
    // $response is null
    ?>
    
    Code (markup):
    Alternate Method - This way is failing also.
    
    <?php 
    $url = "build_xml.php";
    $ch = curl_init();    // initialize curl handle
    curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
    curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); // add POST fields
    $result = curl_exec($ch); // run the whole process
    echo $result; //contains response from server 
    // NO RESPONSE RECEIVED
    ?>
    
    Code (markup):

     
    cesarcesar, Aug 4, 2007 IP
  2. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    	/** 
    	 * Define POST URL and also payload
    	 */
    	define('XML_PAYLOAD', '<?xml version="1.0"?><member><name>name</name></member>');
    	define('XML_POST_URL', 'http://www.domain.com/build_xml.php');
    		
    	/**
    	 * Initialize handle and set options
    	 */
    	$ch = curl_init(); 
    	curl_setopt($ch, CURLOPT_URL, XML_POST_URL); 
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    	curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
    	curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD); 
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    	
    	/**
    	 * Execute the request and also time the transaction
    	 */
    	$start = array_sum(explode(' ', microtime()));
    	$result = curl_exec($ch); 
    	$stop = array_sum(explode(' ', microtime()));
    	$totalTime = $stop - $start;
    	
    	/**
    	 * Check for errors
    	 */
    	if ( curl_errno($ch) ) {
    		$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    	} else {
    		$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		switch($returnCode){
    			case 404:
    				$result = 'ERROR -> 404 Not Found';
    				break;
    			default:
    				break;
    		}
    	}
    	
    	/**
    	 * Close the handle
    	 */
    	curl_close($ch);
    	
    	/**
    	 * Output the results and time
    	 */
    	echo 'Total time for request: ' . $totalTime . "\n";
    	echo $result;  	
    	
    	/**
    	 * Exit the script
    	 */
    	exit(0);
    ?>
    
    PHP:
     
    Chemo, Aug 4, 2007 IP
  3. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Note: the error checking section should actually look something like this:
    
    	/**
    	 * Check for errors
    	 */
    	if ( curl_errno($ch) ) {
    		$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    	} else {
    		$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		switch($returnCode){
    			case 200:
    				break;
    			default:
    				$result = 'HTTP ERROR -> ' . $returnCode;
    				break;
    		}
    	}
    
    PHP:
    Notice the difference is in the switch so it captures the non-200 response code.

    Bobby
     
    Chemo, Aug 4, 2007 IP
  4. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks bobbie. I'm trying now.
     
    cesarcesar, Aug 4, 2007 IP
  5. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    wow it works great. thanks so much.

    i did notice that it only accepts one reply value. can it receive an xml data set like its sending?

    otherwise its perfect. great solution.
     
    cesarcesar, Aug 9, 2007 IP
  6. michaelh613

    michaelh613 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I've tried using this solution and the xml comes over as an array. Is that what we should be expecting. As I'm sending to a third party its hard to know if the issue may be on their end.

    (I'm hoping someone is still getting replies to this thread.
     
    michaelh613, Mar 18, 2008 IP
  7. asault

    asault Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Total time for request: 3.99886918068 cURL ERROR -> 7: couldn't connect to host
    pls help me
     
    asault, May 8, 2012 IP
  8. asault

    asault Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    and some times its showing connect() timed out! ... not getting results pls reply soon
     
    asault, May 8, 2012 IP
  9. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Is the remote server blocking the connection? E.g Firewall
     
    PK-Host, May 8, 2012 IP
  10. asault

    asault Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks for ur reply........ we have already got through the firewall for our corresponding ip




    its my code

    <?php
    /**
    * Define POST URL and also payload
    */
    define('XML_PAYLOAD', '<?xml version="1.0" encoding="UTF-16" standalone="no"?>
    <data>
    <ID>27</ID>
    <REQ_ID>0123456789abcdefg</REQ_ID>
    <ReferenceNo>29684</ReferenceNo>
    <ProcessDate>02/27/12</ProcessDate>
    <ProcessTime>11:18:20 AM</ProcessTime>
    </data>');
    define('XML_POST_URL', 'http://96.253.63.114:25080/msmqdci/receiver.asp');

    /**
    * Initialize handle and set options
    */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));

    /**
    * Execute the request and also time the transaction
    */
    $start = array_sum(explode(' ', microtime()));
    $result = curl_exec($ch);
    $stop = array_sum(explode(' ', microtime()));
    $totalTime = $stop - $start;

    /**
    * Check for errors
    */
    if ( curl_errno($ch) ) {
    $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    } else {
    $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
    switch($returnCode){
    case 404:
    $result = 'ERROR -> 404 Not Found';
    break;
    default:
    break;
    }
    }

    /**
    * Close the handle
    */
    curl_close($ch);

    /**
    * Output the results and time
    */
    echo 'Total time for request: ' . $totalTime . "\n";
    echo $result;

    /**
    * Exit the script
    */
    exit(0);
    ?>
     
    asault, May 8, 2012 IP
  11. asault

    asault Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    No, The problem still exists. Kindly give me a solution!!
     
    asault, May 11, 2012 IP
  12. ayanfe

    ayanfe Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    pls can you explain how i will retrieve it from the target url ..
     
    ayanfe, Jan 22, 2013 IP
  13. ayanfe

    ayanfe Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    i am getting Disallowed Key Characters.
     
    ayanfe, Jan 22, 2013 IP
  14. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #14
    what site are you receiving information responses from? I know UPS has an XML barebones that can be interfaced with PHP in a few simple steps.
     
    ezprint2008, Jan 26, 2013 IP