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
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

Link to comment
Share on other sites

I don't know. I guess you could try it or something.

 

Or perhaps try to figure out the logic. I mean, in what cases would $result === FALSE, causing the loop to continue?

 

Does my example output include the attempt after google.com works?

Link to comment
Share on other sites

Sorry, no patience for those who don't try before they ask ;) ;)

 

I guess I could use nicer ways to encourage that, but it' so much less fun! Best of luck with your script. If there's any logic you need elaborated, feel free to ask.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

It does, but rather than outputting the results of the cURL request, it stores it in the $result variable.

 

Yes, it needs a minor change. $str doesn't contain anything, because your ob_get_contents() returns nothing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.