如何使用cURL获得回复?

时间:2017-10-23 06:30:58

标签: php curl

我正在尝试根据this doc使用curl获得响应。但我是cURL / REST的新手,并且不知道如何做到这一点。

例如,文档中的这段代码表示它返回文件夹列表

curl -X GET -u "email:password" https://www.seedr.cc/rest/folder

但是当我在PHP中尝试它时它不起作用。我怎么能这样做 - >

编辑:看起来这是一个命令行卷曲。我尝试使用命令行并且它可以工作,但我需要知道如何在PHP脚本中使用它。

2 个答案:

答案 0 :(得分:1)

您不需要使用curl。你可以直接在php中完成。

$url="https://www.seedr.cc/rest/folder"

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);

答案 1 :(得分:1)

这是如何在php中使用cURL的示例。您可以使用curl_setopt函数来设置和更改选项以适合您的特殊情况。其中一些实际上是可选的。 You can check the table here to get a full list of what they do

    $durl = "https://www.seedr.cc/rest/folder?email=$password" ;
    $ch =  curl_init()  ;
    curl_setopt($ch,CURLOPT_URL, $durl);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
    $dinf = curl_exec ($ch); 
    if(!curl_errno($ch) ){
        echo $dinf ;

    }else{
        echo curl_error($ch) ;
    }
相关问题