如何为以下CURL命令编写php代码?

时间:2016-04-26 05:34:55

标签: php curl-commandline

如何编写用于以下CURL命令的php代码?

curl -H“授权:持票人oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L”https://api.url.com/v1/market/total-items.json

2 个答案:

答案 0 :(得分:2)

我认为你需要这样的东西:

<?php 

        $url = "https://api.url.com/v1/market/total-items.json"; 
        $page = "/v1/market/total-items.json";
        $headers = array( 
            "POST ".$page." HTTP/1.0", 
            "Authorization: Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L" 
        ); 

        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL,$url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

        $data = curl_exec($ch); 

答案 1 :(得分:1)

您可以从github branch为此cURL命令生成PHP代码。这是为您的请求生成的:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.url.com/v1/market/total-items.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");


$headers = array();
$headers[] = "Authorization: Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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