php

PHP Curl with XML data

Snippet

Instead of using POST key/value variables, use an XML payload, which doesn't require the key.

<?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);

States of US as selected HTML select from array

Snippet

Render an array of USA states as an HTML select / option of USA states, with the current one selected.

/* output directly to screen */
function output_directly($current_state) {
 
  global $states;  // see /snip/states-us-php-array for this array
 
  print "<select name=\"state\">\n";
  print '<option value="" ' . (empty($current_state) ? 'selected="selected"' : '') . ">Select a State</option>\n";
 
  foreach ($states as $abbr => $state) {
    print '<option value="' . $abbr . " " . ($current_state == $abbr ? 'selected="selected"' : '') . '>' . $state . "</option>\n";
  }
 
  print "</select>\n";
}
 
 
/**
 * @param current state
 * @return string of HTML select with states as option
 */
function return_html_string($current_state) { 
 
  // really now, shouldn't $states be a statically defined var?
  global $states;
 
  $c = "<select name=\"state\">\n";
  $c .= '<option value="" ' . (empty($current_state) ? 'selected="selected"' : '') . ">Select a State</option>\n";
 
  foreach ($states as $abbr => $state) {
    $c .= '<option value="' . $abbr . " " . ($current_state == $abbr ? 'selected="selected"' : '') . '>' . $state . "</option>\n";
  }
 
  $c .= "</select>\n";
 
  return $c;
}

States of US as PHP array

Snippet

List of states as a PHP array. Can be used to render nicely in a foreach loop.

$states = array('AL'=>"Alabama",
                'AK'=>"Alaska", 
                'AZ'=>"Arizona", 
                'AR'=>"Arkansas", 
                'CA'=>"California", 
                'CO'=>"Colorado", 
                'CT'=>"Connecticut", 
                'DE'=>"Delaware", 
                'DC'=>"District Of Columbia", 
                'FL'=>"Florida", 
                'GA'=>"Georgia", 
                'HI'=>"Hawaii", 
                'ID'=>"Idaho", 
                'IL'=>"Illinois", 
                'IN'=>"Indiana", 
                'IA'=>"Iowa", 
                'KS'=>"Kansas", 
                'KY'=>"Kentucky", 
                'LA'=>"Louisiana", 
                'ME'=>"Maine", 
                'MD'=>"Maryland", 
                'MA'=>"Massachusetts", 
                'MI'=>"Michigan", 
                'MN'=>"Minnesota", 
                'MS'=>"Mississippi", 
                'MO'=>"Missouri", 
                'MT'=>"Montana",
                'NE'=>"Nebraska",
                'NV'=>"Nevada",
                'NH'=>"New Hampshire",
                'NJ'=>"New Jersey",
                'NM'=>"New Mexico",
                'NY'=>"New York",
                'NC'=>"North Carolina",
                'ND'=>"North Dakota",
                'OH'=>"Ohio", 
                'OK'=>"Oklahoma", 
                'OR'=>"Oregon", 
                'PA'=>"Pennsylvania", 
                'RI'=>"Rhode Island", 
                'SC'=>"South Carolina", 
                'SD'=>"South Dakota",
                'TN'=>"Tennessee", 
                'TX'=>"Texas", 
                'UT'=>"Utah", 
                'VT'=>"Vermont", 
                'VA'=>"Virginia", 
                'WA'=>"Washington", 
                'WV'=>"West Virginia", 
                'WI'=>"Wisconsin", 
                'WY'=>"Wyoming");

Pages