如何将curl写入php?

时间:2017-11-25 11:11:13

标签: php curl

我在PHP脚本下面有这个。我想用curl添加调用函数。 如何在我的下面的脚本上写curl?

我是编程新手。我还在学习它。请检查我的代码并尝试帮助我添加curl。

我的脚本没有错误。只是我想添加php函数替换curl函数。

我希望如此,你理解。抱歉我的英语不好。感谢

我的代码在这里:

$url = 'https://web.facebook.com/'.$pageid.'/videos/'.$id.'/';

$context = [
    'http' => [
        'method' => 'GET',
        'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36",
    ],
];
$context = stream_context_create($context);
$data = file_get_contents($url, false, $context);

function cleanStr($str)
{
    return html_entity_decode(strip_tags($str), ENT_QUOTES, 'UTF-8');
}

function hd_finallink($curl_content)
{

    $regex = '/hd_src_no_ratelimit:"([^"]+)"/';
    if (preg_match($regex, $curl_content, $match)) {
        return $match[1];

    } else {return;}
}

function sd_finallink($curl_content)
{

    $regex = '/sd_src_no_ratelimit:"([^"]+)"/';
    if (preg_match($regex, $curl_content, $match1)) {
        return $match1[1];

    } else {return;}
}


$hdlink = hd_finallink($data);
$sdlink = sd_finallink($data);

if ($sdlink || $hdlink) {

echo '<a href="'.$hdlink.'" download="hd.mp4" class="link">HD Download </a>'; 
echo '<a href="'.$sdlink.'" download="sd.mp4" class="link">SD Download </a>';

} else {

echo 'Download not showing - Please Reload This Page';

}

1 个答案:

答案 0 :(得分:1)

在我们对cURL请求做任何事情之前,我们需要先实例化一个cURL实例 - 我们可以通过调用函数curl_init();

来实现

GET请求的示例代码:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

POST请求的示例代码:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        item1 => 'value',
        item2 => 'value2'
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

See this link