Examples
This page shows you how to use ecom2json API to fetch product data using different programming languages.
Python
Curl
NodeJS
PHP
Go
import requests
import json
import urllib.parse
​
ecom2json_api = "https://api.ecom2json.com"
api_key = "your_api_key"
product_url = "target_product_url"
​
def product_url_to_json():
params = {
"api_key": api_key,
"url": urllib.parse.quote(product_url, safe="")
}
r = requests.get(ecom2json_api, params=params)
if r.status_code == 200:
print(json.loads(r.content))
else:
print("Request Failed!")
print(f"Status Code: {r.status_code}")
print(f"Response: {r.content}")
product_url_to_json()
​
curl https://api.ecom2json.com?api_key=[YOUR_API_KEY]&url=[ENCODED_URL]
const axios = require("axios");
​
const ecom2json_api = "https://api.ecom2json.com";
const api_key = "your_api_key";
const product_url = "target_product_url"
​
axios
.get(ecom2json_api, {
params: {
api_key: api_key,
url: encodeURI(product_url)
}
})
.then((res) => {
if (res.status === 200) {
console.log(res.data);
}
})
.catch((error) => {
console.log("Request Failed!");
if (error.response) {
console.log("Status code: ", error.response.status);
console.log("Response: ", error.response.data);
} else {
console.log("Error: ", error)
}
});
<?php
​
const ECOM2JSON_API = "https://api.ecom2json.com/";
const API_KEY = "your_api_key";
​
function product_url_to_json($url) {
$request_url = sprintf("%s?api_key=%s&url=%s",
ECOM2JSON_API, API_KEY, $url);
​
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_HTTPHEADER,
array( "Content-Type:application/json" )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
​
$response = curl_exec( $ch );
if($response) {
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($status_code == 200) {
echo $response;
}
else {
echo "Request Failed!" . PHP_EOL;
echo "Status Code: " . $status_code . PHP_EOL;
echo "Response: " . $response . PHP_EOL;
}
}
else {
echo "Request Failed!" . PHP_EOL;
echo "Reason: " . curl_error($ch) . PHP_EOL;
}
curl_close( $ch );
}
​
​
$product_url = rawurlencode("target_product_url");
product_url_to_json($product_url);
​
?>
package main
​
import (
"io/ioutil"
"log"
"net/http"
)
​
const ecom2json_api = "https://api.ecom2json.com"
const api_key = "your_api_key"
​
var product_url = "target_product_url"
​
func productUrlToJSON() {
client := &http.Client {}
req, err := http.NewRequest("GET", ecom2json_api, nil)
if err != nil {
log.Print(err)
return
}
​
q := req.URL.Query()
q.Add("api_key", api_key)
q.Add("url", product_url)
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err == nil {
response, _ := ioutil.ReadAll(resp.Body)
​
if resp.StatusCode == 200 {
log.Println(string(response))
} else {
log.Println("Request Failed!")
log.Println("Status Code: ", resp.StatusCode)
log.Println("Response: ", string(response))
}
} else {
log.Println("Request Failed!")
log.Println("Error: ", err)
}
}
​
func main() {
productUrlToJSON()
}
Copy link