如何翻译这个curl命令,使其在PHP脚本中工作


How can I translate this curl command so that it works in a PHP script?

CLI CURL->PHP CURL(仅获取标头(

curl '
 --head '
 --request GET '
 --silent '
 --header 'Authorization: Bearer XXX' '
 http://example.com/remote/foler/file.zip

我只需要获得响应标头,而不需要每次下载整个文件。

我还需要使用GET方法,因为远程服务器限制了我。

只需要采取几个步骤。1.初始化2.设置选项3.执行4.关闭连接

<?php
$url = "www.example.com";
//initilise
$ch = curl_init();
//set the option
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//no body
curl_setopt($ch, CURLOPT_NOBODY, 1);
//set the method 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');    
// request header
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
//execute
$result = curl_exec($ch);
$information = curl_getinfo($ch);
//close the connection
curl_close($ch);

echo '-- REQUEST HEADER --------------------------'.PHP_EOL;
print_r($information['request_header']);
echo '--------------------------------------------'.PHP_EOL;
echo PHP_EOL;
echo '-- RESPONSE HEADER -------------------------'.PHP_EOL;
print_r($result);
echo '--------------------------------------------'.PHP_EOL;

默认情况下,Curl使用GET。如果要将其设置为POST方法,则选项为CURLOPT_POST。您可以设置许多选项,请参阅php.net手册。

据我所知,这不是一个真正的问题,更多的是一项任务http://php.net/manual/es/book.curl.php.特别检查CURLOPT_HEADER

如果你有一个等于或高于5.5的php版本,你可以检查http://guzzle.readthedocs.org/en/latest/overview.html作为cURL上的抽象层。

和往常一样,在Stackoverflow内部搜索,看看问题或类似问题是否已经存在:https://stackoverflow.com/search?q=curl+标头。这将有助于保持问题和答案的质量。