2012-11-08 4 views
0

외부 사이트의 이미지를 내 WordPress 테마의 폴더에 저장하려고합니다. 그리고 나는 곱슬 곱슬 함을 두 번 부르거나 한 번에 끝낼 수 있는지 궁금해하고있었습니다.Wordpress curl save 이미지

예 :

$data = get_url('http://www.veoh.com/watch/v19935546Y8hZPgbZ'); // getting the url first curl instance 
preg_match('/fullHighResImagePath="(.*?)"/', $data, $thumbnail); // find the image from content 
savePhoto($thumbnail, $post->ID); //2nd instance of curl to save the image 

function get_url($url) { 
$user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2)"; 
    $ytc = curl_init(); // initialize curl handle 
    curl_setopt($ytc, CURLOPT_URL, $url); // set url to post to 
    curl_setopt($ytc, CURLOPT_FAILONERROR, 1); // Fail on errors 
    curl_setopt($ytc, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
    curl_setopt($ytc, CURLOPT_RETURNTRANSFER, 1); // return into a variable 
    curl_setopt($ytc, CURLOPT_PORT, 80); //Set the port number 
    curl_setopt($ytc, CURLOPT_TIMEOUT, 15); // times out after 15s 
    curl_setopt($ytc, CURLOPT_HEADER, 1); // include HTTP headers 
    curl_setopt($ytc, CURLOPT_USERAGENT, $user_agent); 
    $source = curl_exec($ytc); 
    curl_close($ytc); 

    $data = trim($source); 
    return $data; 
} 

function savePhoto($remoteImage, $isbn) { 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $remoteImage); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); 
    $fileContents = curl_exec($ch); 
    curl_close($ch); 
    if (DIRECTORY_SEPARATOR=='/'){ 
     $absolute_path = dirname(__FILE__).'/'; 
    } else { 
     $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
    } 
    $newImg = imagecreatefromstring($fileContents); 
    return imagejpeg($newImg, $absolute_path ."video_images/{$isbn}.jpg",100); 
} 

답변

2

사용 Worpress 기능 wp_remote_get 및 워드 프레스는 컬을 사용하여 통화를 처리 할 수 ​​있습니다.

그래서 당신은

$data = my_get_remote_content('http://www.veoh.com/watch/v19935546Y8hZPgbZ'); 
// find the image from content 
preg_match('/fullHighResImagePath="(.*?)"/', $data, $thumbnail); 
//2nd instance of curl to save the image 
savePhoto(my_get_remote_content($thumbnail), $post->ID); 

function my_get_remote_content($url) { 
    $response = wp_remote_get($url, 
    array(
     'headers' => array(
     'user-agent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2)' 
    ) 
    ) 
); 
    if(is_wp_error($response)) { 
    throw new Exception('Error fetching remote content'); 
    } else { 
    $data = wp_remote_retrieve_body($response); 
    return $data; 
    } 
} 

function savePhoto($fileContents, $isbn) { 
    if (DIRECTORY_SEPARATOR=='/'){ 
    $absolute_path = dirname(__FILE__).'/'; 
    } else { 
    $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
    } 
    $newImg = imagecreatefromstring($fileContents); 
    return imagejpeg($newImg, $absolute_path ."video_images/{$isbn}.jpg",100); 
} 
+0

Thnx Air4x처럼 생명의 은인을 뭔가를 할 수 있습니다. 나는 4 년 동안 wordpress를 사용했고, PHP에서 5 가지 옵션을 확인하는 plus 함수를 내장했다는 것을 결코 알지 못했다. 내가 충분한 평판을 가지고 있다면 나는 당신의 답장에 투표 할 것이다. :) –