Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SimpleXML to cURL

Tags:

php

wordpress

My host has allow_url_fopen disabled and they will not enable it for me. I need the following code to work for a WordPress plugin. Can someone please give me pointers as to how to convert this code to cURL?

else:
    $results = $wpdb->get_results($wpdb->prepare("SELECT mcd_id FROM mcd_cl_coupons WHERE coupon_list_id = %d AND created_by = 'mcd'", array($coupon_list_id)));
    $mcd_id = ''; foreach($results as $row): $mcd_id .= '-' . $row->mcd_id; endforeach; $mcd_id = substr($mcd_id, 1);
    if($mcd_id): 
        $options = get_option('mcd_list');
        $token = $options['api_key'];
        $xml_file = 'http://www.mycoupondatabase.com/api/coupons-xml.php?token=' . $token . '&id_string=' . $mcd_id;
        $xml = simplexml_load_file($xml_file);
    endif;
like image 415
Mitchell Avatar asked Jul 19 '26 21:07

Mitchell


1 Answers

Wordpress has a function build in to request a file called wp_remote_get:

    $xml_file = 'http://www.mycoupondatabase.com/api/coupons-xml.php?token=' . $token . '&id_string=' . $mcd_id;
    $xml_data = wp_remote_get($xml_file);
    $xml = simplexml_load_string($xml_data['body']);

That function internally makes use of the HTTP abstraction wordpress comes with which normally figures out the best way to do HTTP requests for the system it's running on. So it will use cUrl if everything else is restricted on your host.

like image 86
hakre Avatar answered Jul 22 '26 10:07

hakre