将Curl Sheel代码转换为PHP

时间:2017-12-05 07:44:49

标签: php json curl

我想将Curl Shell代码转换为PHP代码,但我收到一个错误,即在URL中找到非法字符

<?php 
    $url = "https://www.unocoin.com/api/v1/general/prices";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, "Content-Type:application/json
    -H");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST
    -H");

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
?>

我希望json格式显示在屏幕上

The Output of above code

2 个答案:

答案 0 :(得分:0)

您要将标题传递给CURLOPT_URL,标题应设置在CURLOPT_HTTPHEADER

试试这个

<?php

    $url = "https://www.unocoin.com/api/v1/general/prices";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    } else {
        echo $result;
    }

    curl_close ($ch);
?>

答案 1 :(得分:0)

简要介绍一下api文档,您需要设置授权标头并设置内容类型并发出POST请求。我希望以下内容能提供一些指导 - 尽管由于没有有效的不记名令牌,我无法完全测试。

<?php

    $url='https://www.unocoin.com/api/v1/general/prices';
    $token='ef0e897abb0c573fbed4bcf617bfb9c0e8b34bb3';
    $result=unicoinrequest( $url, $token );

    if( $result->info->http_code==200 ){

        echo '<pre>',print_r( $result->response,true ),'</pre>';

    } else {
        echo '<pre>',print_r( $result->info,true ),'</pre>';
        echo '<pre>',print_r( $result->errors,true ),'</pre>';
    }


    function unicoinrequest( $url=false, $token=false ){
        /*
            Download a copy of cacert.pem from
            https://curl.haxx.se/ca/cacert.pem
        */
        $cacert='c:/wwwroot/cacert.pem';#   <----- set to applicable location

        $res=array(
            'response'  =>  null,
            'info'      =>  array( 'http_code' => 0 ),
            'headers'   =>  null,
            'errors'    =>  null,
            'verbose'   =>  null
        );
        if( empty( $url ) ) return (object)$res;

        $vbh = fopen('php://temp', 'w+');

        session_write_close();

        /* Initialise curl request object */
        $curl=curl_init();
        if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
            curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
            curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
        }

        /* Define standard options */
        curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
        curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $curl, CURLOPT_FAILONERROR, true );
        curl_setopt( $curl, CURLOPT_HEADER, false );
        curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
        curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
        curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
        curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
        curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
        curl_setopt( $curl, CURLOPT_ENCODING, '' );


        curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST' );
        curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: Bearer ' . $token ) );


        curl_setopt( $curl,CURLOPT_VERBOSE,true );
        curl_setopt( $curl,CURLOPT_NOPROGRESS,true );
        curl_setopt( $curl,CURLOPT_STDERR,$vbh );


        /* Execute the request and store responses */
        $res=(object)array(
            'response'  =>  curl_exec( $curl ),
            'info'      =>  (object)curl_getinfo( $curl ),
            'errors'    =>  curl_error( $curl )
        );
        curl_close( $curl );

        rewind( $vbh );
        $res->verbose=stream_get_contents( $vbh );
        fclose( $vbh );

        return $res;
    }

?>

结果

stdClass Object
(
    [url] => https://www.unocoin.com/api/v1/general/prices
    [content_type] => 
    [http_code] => 401
    [header_size] => 0
    [request_size] => 268
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.747
    [namelookup_time] => 0
    [connect_time] => 0.281
    [pretransfer_time] => 0.827
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 1.747
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)
The requested URL returned error: 401