Php Wordpress curl保存图像
我正在将外部站点的图像保存到wordpress主题的文件夹中。我想知道给curl打两次电话是可以的,还是一次就可以 例如:Php Wordpress curl保存图像,php,wordpress,curl,preg-match,Php,Wordpress,Curl,Preg Match,我正在将外部站点的图像保存到wordpress主题的文件夹中。我想知道给curl打两次电话是可以的,还是一次就可以 例如: $data = get_url('http://www.veoh.com/watch/v19935546Y8hZPgbZ'); // getting the url first curl instance preg_match('/fullHighResImagePath="(.*?)"/', $data, $thumbnail); // find the image fr
$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);
}
使用Worpress函数,让Wordpress使用curl处理调用
所以你可以做一些像
$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);
}
使用Worpress函数,让Wordpress使用curl处理调用
所以你可以做一些像
$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);
}
Thnx Air4x是您的救生圈。我使用wordpress已经4年了,从来都不知道他们内置了这个函数,还检查了php中的5个不同选项。如果我有足够的声誉,我会投票支持你的回答:)Thnx Air4x你的救命稻草。我使用wordpress已经4年了,从来都不知道他们内置了这个函数,还检查了php中的5个不同选项。如果我有足够的声誉,我会投票支持你的回答:)