Kristoff1875 Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/ Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364252 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 Will that only try the second if the first doesn't work? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364259 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364261 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 Well, thanks for the great help (genuine) and the snooty follow up (sarcasm)! Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364262 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364265 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 I normally would have tried before doing anything, but was on hold with Go Daddy for half an hour and had literally just been connected as I read your post. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364269 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364275 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 It may also be important to mention i'm posting the url with the username etc at the end, for example: url1?strUserName=username&strPassword=password&etc Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364278 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); changes the way your script works. Rather than use output buffering, the response is stored in the $result variable. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364290 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364291 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364293 Share on other sites More sharing options...
Kristoff1875 Posted July 25, 2012 Author Share Posted July 25, 2012 Perfect thank you. Quote Link to comment https://forums.phpfreaks.com/topic/266224-backup-url-for-remote-xml-request/#findComment-1364295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.