PHP curl_init Curl message header and message body

Source: Internet
Author: User
Keywords Php curl
Given a remote picture address (HTTP address) and then use Curl to request that the remote picture exists by checking the returned message header
But there's a serious efficiency problem here is the message body.
The body of the message returned the picture. There are settings that can only generate the message body so that the message body is empty or does not produce the message body?

//检查远程文件function checkRemoteFile($file_name, $path) {    $path = $path . "/" . $file_name;    $ch = curl_init();    $timeout = 30;  //在尝试连接时等待的秒数。设置为0,则无限等待。    curl_setopt($ch, CURLOPT_URL, $path);    curl_setopt($ch, CURLOPT_HEADER, 1);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);    $contents = curl_exec($ch);//echo $contents;    if (preg_match("/404/", $contents)) {        echo '0';        return false;    } elseif(preg_match("/200/", $contents)) {        echo $contents;        return true;    

Reply content:

Given a remote picture address (HTTP address) and then use Curl to request that the remote picture exists by checking the returned message header
But there's a serious efficiency problem here is the message body.
The body of the message returned the picture. There are settings that can only generate the message body so that the message body is empty or does not produce the message body?

//检查远程文件function checkRemoteFile($file_name, $path) {    $path = $path . "/" . $file_name;    $ch = curl_init();    $timeout = 30;  //在尝试连接时等待的秒数。设置为0,则无限等待。    curl_setopt($ch, CURLOPT_URL, $path);    curl_setopt($ch, CURLOPT_HEADER, 1);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);    $contents = curl_exec($ch);//echo $contents;    if (preg_match("/404/", $contents)) {        echo '0';        return false;    } elseif(preg_match("/200/", $contents)) {        echo $contents;        return true;    

No curl is OK, fopen seems to be able to open the remote file.

$hd = fopen($remoteUrl, 'r');if ($hd === false) die('404')else fclose($hd);

Oh, see your comment, with curl_setopt ($ch, Curlopt_nobody, true); Look

Isn't curl okay? You try this!

public function checkRemoteHttpFileExists($url) {    $curl = curl_init($url);    // 不取回数据    curl_setopt($curl, CURLOPT_NOBODY, true);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');    // @curl_setopt($curl, CUROPT_RETURNTRANSFER,1);    // 发送请求    $result = @curl_exec($curl);    $found = false;    // 如果请求没有发送失败    if ($result !== false) {        // 再检查http响应码是否为200        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);        if ($statusCode == 200) {            $found = true;        }    }    curl_close($curl);    return $found;}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.