2017-02-20 63 views
0

如何在WordPress中插入php curl函數。我已經試過這個PHP代碼,它按預期工作。如何在WordPress中轉換相同的捲曲功能。如何在wordpress中放置CURL

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://api.bstcm.no/customer/-8/offers"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
$username = 'atrium', 
$password = '3HnzkG2p', 
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password) 
)); 
$response = curl_exec($ch); 
curl_close($ch); 
$details = json_decode($response, TRUE); 
?> 

我嘗試這樣做:

<?php 
$response = wp_remote_get('http://api.bstcm.no/customer/-8/offers', array(

    'headers' => array(), 
    'body' => array('username' =>'atrium','password' => '3HnzkG2p') 
    ) 
); 

if (is_wp_error($response)) { 
    $error_message = $response->get_error_message(); 
    echo "Something went wrong: $error_message"; 
} else { 
    echo 'response:<pre>'; 
    print_r($response); 
    echo '</pre>'; 
} 
?> 

回答

0

HTTP認證要求要麼用戶名和密碼中的URL被傳遞(不再reccomended)或待Base64編碼中的「授權」首標開始與編碼「基本「和分隔用戶名和密碼的冒號(:):

<?php 
$response = wp_remote_get('http://api.bstcm.no/customer/-8/offers', array(
    'headers' => array(
     'Authorization' => 'Basic ' . base64_encode('atrium:3HnzkG2p') 
    ) 
); 

if (is_wp_error($response)) { 
    $error_message = $response->get_error_message(); 
    echo "Something went wrong: $error_message"; 
} else { 
    echo 'response:<pre>'; 
    print_r($response); 
    echo '</pre>'; 
} 
?>