将命令卷曲转换为PHP卷曲

时间:2015-07-31 23:04:03

标签: php curl

我想将以下命令CURL转换成P是否有在线转换?或者有人可以解释如何转换

curl "http://nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=CNX"%"20NIFTY&fromDate=27-07-2015&toDate=27-07-2015"
<p>-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"</p>
<p>-H "Cache-Control: max-age=0"</p>
<p>-H "Accept-Encoding: gzip, deflate, sdch"</p>
<p>-H "Proxy-Connection: keep-alive"</p>
<p>-H "Accept-Language: en-GB,en-US;q=0.8,en;q=0.6"</p>
<p>-H "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"</p>
<p>--compressed</p>

我添加了p标签以使其看起来干净,请忽略它。

2 个答案:

答案 0 :(得分:1)

这是转换为PHP,您可以使用选项CURLOPT_HTTPHEADER来设置自定义请求标头。

$curl = curl_init();
$headers = array(
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Cache-Control: max-age=0',
    'Accept-Encoding: gzip, deflate, sdch',
    'Proxy-Connection: keep-alive',
    'Accept-Language: en-GB,en-US;q=0.8,en;q=0.6'
);
$opts = array(
    CURLOPT_URL => 'http://nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=CNX"%"20NIFTY&fromDate=27-07-2015&toDate=27-07-2015',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36',
    CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($curl, $opts);
$response = curl_exec($curl);

答案 1 :(得分:1)

下面的代码会像您一样创建请求,如果您需要更多卷曲选项,请查看http://php.net/manual/en/function.curl-setopt.php

$headers = [
 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
 "Cache-Control: max-age=0",
 "Accept-Encoding: gzip, deflate, sdch",
 "Proxy-Connection: keep-alive",
 "Accept-Language: en-GB,en-US;q=0.8,en;q=0.6",
 "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"];

$ch = curl_init('http://nseindia.com/products/dynaContent/equities/indices/historicalindices.jsp?indexType=CNX"%"20NIFTY&fromDate=27-07-2015&toDate=27-07-2015');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_ENCODING , "");
$response = curl_exec($ch);