1

How to write php code for following CURL command?

curl -H "Authorization: Bearer oVi4yPxk1bJ64Y2qOsLJ2D2ZlC3FpK4L" https://api.url.com/v1/market/total-items.json

pskkar
  • 432
  • 1
  • 4
  • 18

2 Answers2

2

I think you need something like this:

<?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); 
Ahmad
  • 5,551
  • 8
  • 41
  • 57
  • 1
    Of course it is not showing anything. I did not output anything in this script. It just stores the result in `$data`. You may add `echo $data;` to see the response – Ahmad Apr 26 '16 at 17:45
  • You're welcome! Please mark the best answer as accepted in order to help future visitors find the best one. – Ahmad Apr 28 '16 at 17:02
1

You can generate PHP code for your cURL command here from a github branch. Here is the one generated for your request:

<?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);
Thamilhan
  • 13,040
  • 5
  • 37
  • 59