Proxy API(single custom php) does not work from Mozilla firefox browser and working with chrome and postman
Get the solution ↓↓↓Here is the my custom php file as a proxy api. Every thing is working in Chrome but not found error even i directly access a get request from browser url.
I am accessing the api from Angular ts. So when i access directly the server(Yii2 framework used) without proxy api it is working for me. So what i need to add to this file so the Mozila can access my api's
class Api
{
/**
* Get response from url
* @param $url
* @param $params
* @param $header
* @param $body
* @return bool|string|null
*/
public static function getResponse($url, $params, $header, $body)
{
if($params)
$url = $url . '?' . $params;
$referer = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$userAgent = 'ZvaApiProxy/1.0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // Good leeway for redirections.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR , "cookie.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_URL, trim($url));
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
http_response_code($httpcode);
curl_close($ch);
if (!isset($response))
return null;
return $response;
}
/**
* Get headers
* @return array|false|string
*/
public function getHeaders()
{
return getallheaders();
}
/**
* Get params
* @return string
*/
public function getParams()
{
return http_build_query($_GET);
}
/**
* Get path info
* @return mixed|string
*/
public function getPathInfo()
{
if(isset($_SERVER['REDIRECT_URL'])){
$str = substr($_SERVER['REDIRECT_URL'], 11);
$_SERVER['REDIRECT_URL'] = $str;
}
return !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (!empty($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : '');
// return $_SERVER['REDIRECT_URL'];
}
public function getAction(){
}
/**
* Get body
* @return string
*/
public function getBody()
{
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
return http_build_query($_GET);
break;
case 'POST':
return http_build_query($_POST);
break;
case 'DELETE':
return http_build_query($_GET);
break;
case 'PUT':
return http_build_query($_POST);
break;
}
}
}
$modal = new Api();
$baseUrl = 'http://dev-api.abc';
echo $modal::getResponse($baseUrl . $modal->getPathInfo(), $modal->getParams(), $modal->getHeaders(), $modal->getBody());
Answer
Solution:
Please comment out the lines i have commented this working for me
<?php
if (isset($_SERVER['HTTP_HOST'])) {
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 10'); // cache for 1 day
}
class Api
{
/**
* Get response from url
* @param $url
* @param $params
* @param $header
* @param $body
* @return bool|string|null
*/
public static function getResponse($url, $params, $header, $body)
{
if($params)
$url = $url . '?' . $params;
//$referer = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//$userAgent = 'ZvaApiProxy/1.0';
$ch = curl_init();
// curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
// curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
// curl_setopt($ch, CURLOPT_TIMEOUT, 20);
// curl_setopt($ch, CURLOPT_REFERER, $referer);
// curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
// curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // Good leeway for redirections.
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR , "cookie.txt");
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_URL, trim($url));
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
// for error
//curl_setopt($ch, CURLOPT_FAILONERROR, true);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
http_response_code($httpcode);
curl_close($ch);
if (!isset($response))
return null;
return $response;
}
/**
* Get headers
* @return array|false|string
*/
public function getHeaders()
{
return getallheaders();
}
/**
* Get params
* @return string
*/
public function getParams()
{
return http_build_query($_GET);
}
/**
* Get path info
* @return mixed|string
*/
public function getPathInfo()
{
if(isset($_SERVER['REDIRECT_URL'])){
$str = substr($_SERVER['REDIRECT_URL'], 11);
$_SERVER['REDIRECT_URL'] = $str;
}
return !empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : (!empty($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : '');
// return $_SERVER['REDIRECT_URL'];
}
public function getAction(){
}
/**
* Get body
* @return string
*/
public function getBody()
{
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
return http_build_query($_GET);
break;
case 'POST':
return http_build_query($_POST);
break;
case 'DELETE':
return http_build_query($_GET);
break;
case 'PUT':
return http_build_query($_POST);
break;
}
}
}
$modal = new Api();
$baseUrl = 'http://dev-api.zoova.ro';
echo $modal::getResponse($baseUrl . $modal->getPathInfo(), $modal->getParams(), '', $modal->getBody());
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.