Jump to content

Backup URL for remote XML request?


Kristoff1875

Recommended Posts

Currently using the following which is working fine:

 

//set POST variables
$url = 'primaryurl.com';
$fields = array(
            'strUserName'=>urlencode('***'),
            'strPassword'=>urlencode('***'),
            'strClientRef'=>urlencode('***'),
            'strClientDescription'=>urlencode('***'),
            'strKey1'=>urlencode('***'),
            'strVersion'=>urlencode('***'),
            'strVRM'=>urlencode(***)
        );


//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

 

How would I go about adding in a backup url incase the first is down? Can I add it to the $url tag? Or would I be best to add $backupurl and then using it in the following part:

 

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

 

Saying if $url gives a response then use it, if not then use $backupurl?

 

Any help appreciated massively.

Link to comment
https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/
Share on other sites

<?php

$feeds = array('arhaerhar.com','ltyfukfnyf.com','google.com','arhaergae.com');

do {
$ch = curl_init();
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1); // 1 second timeout I'm impatient
curl_setopt($ch,CURLOPT_URL,current($feeds).'/index.html');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
//curl_setopt($ch,CURLOPT_POST,count($fields));
//curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
echo 'Trying '.current($feeds).'/index.html<br>';
$result = curl_exec($ch);
} while( $result === FALSE && next($feeds) !== FALSE );

if( $result === FALSE )
echo 'No feeds worked';
else
echo current($feeds).' feed worked';

?>

 

Trying arhaerhar.com/index.html
Trying ltyfukfnyf.com/index.html
Trying google.com/index.html
google.com feed worked

Here's what i've tried:

 

$feeds = array('url1?','url2?');

$fields = array(
            'strUserName'=>urlencode('username'),
            'strPassword'=>urlencode('password'),
            'strClientRef'=>urlencode('ref'),
            'strClientDescription'=>urlencode('desc'),
            'strKey1'=>urlencode('key'),
            'strVersion'=>urlencode('version'),
            'strVRM'=>urlencode($vrm)
        );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');


do {
$ch = curl_init();
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1); // 1 second timeout I'm impatient
curl_setopt($ch,CURLOPT_URL,current($feeds));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

$result = curl_exec($ch);
} while( $result === FALSE && next($feeds) !== FALSE );

curl_close($ch);

$str = ob_get_contents();
ob_end_clean();

 

The errors etc are handled by the next page:

 

But seemingly it's not grabbing any info with what i'm using.

Ah ok, so does it doesn't grab the results from the xml? As the page that calls the page that has the code above, then calls the results as follows:

 

$xml = simplexml_load_string($str);

$vrm = (string)$xml->DataArea->Vehicles->Vehicle->VRM_Curr; 
$make = (string)$xml->DataArea->Vehicles->Vehicle->DVLA_Make;
$model = (string)$xml->DataArea->Vehicles->Vehicle->DVLA_Model; 

 

So would that mean the way that handles need changing?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.