How to write php code for following CURL command?

390 Views Asked by At

How to write php code for following CURL command?

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

2

There are 2 best solutions below

2
On

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); 
0
On

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);