0

I am doing research for a university task where we need to run this cURL code in PHP. Is there any way that this can be done? What's the right syntax?

curl -X GET "https://secure.fusebill.com/v1/customers/{id}/Overview" \
    -H "Content-Type: application/json" \
    -H "Authorization: Basic {APIKey}"
  • 3
    Does this answer your question? [PHP + curl, HTTP POST sample code?](https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) – ArSeN Dec 19 '20 at 22:00
  • Can you add what you have already tried and what didn't work? – endo64 Dec 19 '20 at 22:09
  • @endo64 - I am trying to figure out what's the right syntax – Emanuil Yakimov Dec 19 '20 at 22:10
  • emptystring is not valid json. `{}` is valid json, `""` is valid json, an empty string is not. either remove `Content-Type: application/json`, or add `--data-raw '{}'` – hanshenrik Dec 20 '20 at 01:10

2 Answers2

1

Here is one way to do it with curl and an options array:

<?php

$curl = curl_init("https://secure.fusebill.com/v1/customers/{id}/Overview");

curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic {APIKey}",
    "Content-Type: application/json"
  )
));

$response = curl_exec($curl);
curl_close($curl);

You can alternatively set each option by calling curl_setopt($curl, OPTION_NAME, "value"); for each option in place of curl_setopt_array();:

$curl = curl_init("https://secure.fusebill.com/v1/customers/{id}/Overview");

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Authorization: Basic {APIKey}",
    "Content-Type: application/json"
  ));

$response = curl_exec($curl);
curl_close($curl);

For this request in PHP, you initialize curl and store in a variable so you can add options to the request before executing.

Here is a breakdown of the options from the snippet above:
CURLOPT_RETURNTRANSFER - return data from the request as a string instead of outputting directly (useful if you need to use the data in another function somewhere).
CURLOPT_CUSTOMREQUEST - HTTP request method for the request
CURLOPT_HTTPHEADER - set headers for the request.

Here is how the PHP maps to the cURL above:
-X specifies the HTTP method and the URL. In PHP we set CURLOPT_CUSTOMREQUEST and set the URL when we initialize the cURL handler, you can optionally use CURLOPT_URL to set the URL instead.
-H - stands for headers for the request. In PHP we set CURLOPT_HTTPHEADER. We set the headers as an array since there are multiple headers.

Remember to replace {id} in the URL and {APIKey} in the authorization header of your request.

Shakima F
  • 118
  • 8
0

I will give you some examples you can Just copy and paste in your PHP file to check. First this is my favorite and most efficient way to get information from other webpages or even insert information if you need to. GET RESULTS FROM WEBPAGE BY GET METHOD(It would work for the work you need):

$ch = CURL_INIT();
$url = 'https://google.com';
CURL_SETOPT($ch, CURLOPT_URL, $url );
//CURL_SETOPT($ch, CURLOPT_PROXY, $ip); //IN CASE YOU NEED TO USE PROXY
//CURL_SETOPT($ch, CURLOPT_PROXYPORT, $port);
CURL_SETOPT($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0');
CURL_SETOPT($ch, CURLOPT_POST, 0);//Get instead of post
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER, True);
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION, True);
CURL_SETOPT($ch, CURLOPT_ENCODING, 'gzip, deflate');//Try to curl https://amazon.com WITHOUT THIS LINE, it would give you some extra encryption.
CURL_SETOPT($ch, CURLOPT_CONNECTTIMEOUT,90);
CURL_SETOPT($ch, CURLOPT_TIMEOUT,90); 
$result = CURL_EXEC($ch);
echo $result;

Next is if you need to insert data in the page using a post method. You can use this to LOG IN in some websites or you use to insert and get data automatically

$data = array(
        "email" => "example@gmail.com",
        "pwd" => "12341234",
        "some other field"=> '123123'
);
$ch = CURL_INIT();
$url = 'https://google.com';
CURL_SETOPT($ch, CURLOPT_URL, $url );
CURL_SETOPT($ch, CURLOPT_POST, true); //Post request
CURL_SETOPT($ch, CURLOPT_POSTFIELDS, $data); 
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER,True);
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION,True);
CURL_SETOPT($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) ."/cookie.txt");//I am saving here the cookies in case I need to go to another page or do some action after login
CURL_SETOPT($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) ."/cookie.txt");
CURL_SETOPT($ch, CURLOPT_FOLLOWLOCATION, true); //ALLOW REDIRECTION
CURL_SETOPT($ch, CURLOPT_CONNECTTIMEOUT,90);
CURL_SETOPT($ch, CURLOPT_TIMEOUT,90); 
$result = CURL_EXEC($ch);

Second it is the most easy one that has some limitations and because of that I don't like:

$url = 'https://www.google.com/';
echo file_get_contents($url);

If you have any questions, let me know. I work with curl all the time!

Hygison Brandao
  • 126
  • 1
  • 8