Jump to content

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

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?

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?

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.