PHP中的Curl Post返回null

时间:2017-01-19 20:42:13

标签: php curl

我有一个卷曲帖子,我可以通过我的控制台成功执行:

curl -i -H 'Accept: application/vnd.twitchtv.v3+json' -H 'Client-ID: XXX' 'https://api.twitch.tv/kraken/channels/EisohneWaffel'

返回一个json文件。

现在我试图通过php发布相同的内容并返回NULL ::

   /* Init result array */
    $result = array();

    /* curl options */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/kraken/channels/EisohneWaffel');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: application/vnd.twitchtv.v3+json',
        'Client-ID: '.Constants::TWITCH_API_CLIENT
    ));
    $result['data'] = curl_exec($ch);
    $result['info'] = curl_getinfo($ch);
    curl_close($ch);

    /* return result array */
    var_dump($result);

$result['data']为空,$result['info']有一些信息:

"info" => array:26 [▼
    "url" => "https://api.twitch.tv/kraken/channels/EisohneWaffel"
    "content_type" => null
    "http_code" => 0
    "header_size" => 0
    "request_size" => 0
    "filetime" => -1
    "ssl_verify_result" => 1
    "redirect_count" => 0
    "total_time" => 0.468
    "namelookup_time" => 0.015
    "connect_time" => 0.203
    "pretransfer_time" => 0.0
    "size_upload" => 0.0
    "size_download" => 0.0
    "speed_download" => 0.0
    "speed_upload" => 0.0
    "download_content_length" => -1.0
    "upload_content_length" => -1.0
    "starttransfer_time" => 0.0
    "redirect_time" => 0.0
    "redirect_url" => ""
    "primary_ip" => "XXX"
    "certinfo" => []
    "primary_port" => 443
    "local_ip" => "192.168.1.243"
    "local_port" => 55581

我错的任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个,curl_exce在成功时返回TRUE,在失败时返回FALSE

    $header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
    $result['header'] = substr($response, 0, $header_size);
    $result['body'] = substr( $response, $header_size );
    $result['http_code'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    $result['last_url'] = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);