开发者

Convert SimpleXML to cURL

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;


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.


I suppose you just want to be able to load the file from external an external URL using cURL. I have this function lying around, which I use sometimes for simple things like this - feel free to modify and re-use it.

define('SLEEP_TIME', 0);
function get_page_by_curl($searchUrl, $post=false, $postParams="")
{
    print " " . $searchUrl;
    global $errMsg;
    $userAgent = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.5 Robot";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL, $searchUrl);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_NOPROGRESS, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    if($post)
    {
         curl_setopt ($ch, CURLOPT_POST, true);
         curl_setopt ($ch, CURLOPT_POSTFIELDS, $postParams);
    }
    $htmlPage = false;
    do
    {
        $htmlPage = curl_exec($ch);
        $errno = curl_errno($ch);
        if($errno == 28)
        {
            print ".";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno == 7)
        {
            print "*";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno == 6)
        {
            print "+";
            flush();
            sleep(SLEEP_TIME);
        }
        elseif($errno != 0)
        {
            $errMsg = $errno . ": " . curl_error($ch);
        }
    }
    while(!$htmlPage && ($errno == 28 || $errno == 6 || $errno == 7));

    return $htmlPage;
}

Then from your code, change line

$xml = simplexml_load_file($xml_file);

to

$sml = get_page_by_curl($xml_file);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜