PHP - Insert a variable into an array -
say have array
$args = array('responsetype' => 'xml', 'servername' => 'vl18278.dinaserver.com', 'command' => 'vps_getusedspace', ) ;
this array composes url send through curl. need replace vl18278.dinaserver.com
variable $vps, when replace it, url show %5b0%5d
before = sign of attribute servername:
responsetype=xml&servername%5b0%5d=vl18278.dinaserver.com&command=vps_getusedspace
if dont replace vl18278.dinaserver.com
, url correct.
what wrong code? why %5b0%5d getting url? :( in advance.
complete code:
<?php $listavps = simplexml_load_file('servers.xml'); foreach ($listavps->servers->server $vps) { $urlapi = 'url.php'; $username = 'user'; $password = 'pass'; $args = array('responsetype' => 'xml', 'servername' => 'vl18278.dinaserver.com', 'command' => 'vps_getusedspace', ) ; $args = ( is_array ( $args ) ? http_build_query ( $args, '', '&' ) : $args ); $headers = array(); $handle = curl_init($urlapi); if( $handle === false ) // error starting curl { $error = '0 - couldn\'t start curl'; } else { curl_setopt ( $handle, curlopt_followlocation, true ); curl_setopt ( $handle, curlopt_returntransfer, true ); curl_setopt ( $handle, curlopt_url, $urlapi ); curl_setopt( $handle, curlopt_userpwd, $username.':'.$password ); curl_setopt( $handle, curlopt_httpauth, curlauth_basic ); curl_setopt( $handle, curlopt_timeout, 60 ); curl_setopt( $handle, curlopt_connecttimeout, 4); // set higher if "28 - ssl connection timeout" error curl_setopt ( $handle, curlopt_header, true ); curl_setopt ( $handle, curlopt_httpheader, $headers ); $curlversion = curl_version(); curl_setopt ( $handle, curlopt_useragent, 'php '.phpversion().' + curl '.$curlversion['version'] ); curl_setopt ( $handle, curlopt_referer, null ); curl_setopt ( $handle, curlopt_ssl_verifypeer, false ); // set false if "60 - ssl certificate problem" error curl_setopt ( $handle, curlopt_postfields, $args ); curl_setopt ( $handle, curlopt_post, true ); $response = curl_exec ( $handle ); echo $args; if ($response) { $response = substr( $response, strpos( $response, "\r\n\r\n" ) + 4 ); // remove http headers // parse response $responsesimplexml = simplexml_load_string($response); if( $responsesimplexml === false ) { // invalid xml response } else { // parse response $errorcode = $responsesimplexml->response->responsecode ; echo $errorcode; if( $errorcode == 1000 ) // success { $usado = $responsesimplexml->response->data->total_space; $capacidad = $responsesimplexml->response->data->space_limit; echo 'usado: '.$usado.'</br>total: '.$capacidad.'.'; } else // normal errors { $errors = $responsesimplexml->response->errors; foreach( $errors->error $error ) { // process error } } } } else // http response code != 200 { $error = curl_errno ( $handle ) . ' - ' . curl_error ( $handle ); } curl_close($handle); } } ?>
your variable $server
must array, because, once decoded, %5b0%5d
[0]
.
my guess use $server[0]
instead of $server
wherever replace value. without replacement code, hard determine.
Comments
Post a Comment