Open jianglin8-hub opened 2 weeks ago
Again this is way too short description.
First of all i'm not a professional programmer, just a hobby You are trying to make this library work with so many connections that i'm wondering what you are trying to do.
So i don't understand what curl mux even mean and i'm not going to implant it either It is too complicated for me, sorry
Again this is way too short description.
First of all i'm not a professional programmer, just a hobby You are trying to make this library work with so many connections that i'm wondering what you are trying to do.
So i don't understand what curl mux even mean and i'm not going to implant it either It is too complicated for me, sorry
Each method call in the class will create a new Request object, and Curl will be initialized in the Request object. What I mean is that the Request object can be reused to reduce the time spent on connecting to the imdb api server
i understand now what you mean and i do share this thought to reduce time but i don't know how that works. And it has a lot of massive changes to make this work. And the amount of API calls will happen in a shorter time which can mean a server overload or even ban. You already seen that with return code 429.
so i don't think this is a solution even if i can make it work which i probably can't
i understand now what you mean and i do share this thought to reduce time but i don't know how that works. And it has a lot of massive changes to make this work. And the amount of API calls will happen in a shorter time which can mean a server overload or even ban. You already seen that with return code 429.
so i don't think this is a solution even if i can make it work which i probably can't
The imdb api responds with 429 because of too many requests. I am not sure about the exact request frequency. If you only request a single imdb information, you will not encounter this problem.
class GraphQL
{
/**
* @var CacheInterface
*/
private $cache;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var Config
*/
private $config;
/**
* @var Request
*/
private $request;
/**
* GraphQL constructor.
* @param CacheInterface $cache
* @param LoggerInterface $logger
* @param Config $config
*/
public function __construct($cache, $logger, $config)
{
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->request = new Request('https://api.graphql.imdb.com/');
$this->request->addHeaderLine("Content-Type", "application/json");
if ($this->config->useLocalization === true) {
if (!empty($this->config->country)) {
$this->request->addHeaderLine("X-Imdb-User-Country", $this->config->country);
}
if (!empty($this->config->language)) {
$this->request->addHeaderLine("X-Imdb-User-Language", $this->config->language);
}
}
}
public function query($query, $qn = null, $variables = array())
{
$key = "gql.$qn." . ($variables ? json_encode($variables) : '') . md5($query) . ".json";
$fromCache = $this->cache->get($key);
if ($fromCache != null) {
return json_decode($fromCache);
}
// strip spaces from query due to hosters request limit
$fullQuery = implode("\n", array_map('trim', explode("\n", $query)));
$result = $this->doRequest($fullQuery, $qn, $variables);
$this->cache->set($key, json_encode($result));
return $result;
}
/**
* @param string $query
* @param string|null $queryName
* @param array $variables
* @return \stdClass
*/
private function doRequest($query, $queryName = null, $variables = array())
{
$payload = json_encode(
array(
'operationName' => $queryName,
'query' => $query,
'variables' => $variables
)
);
$this->logger->info("[GraphQL] Requesting $queryName");
$this->request->post($payload);
if (200 == $this->request->getStatus()) {
return json_decode($this->request->getResponseBody())->data;
} else {
$this->logger->error(
"[GraphQL] Failed to retrieve query [{queryName}]. Response headers:{headers}. Response body:{body}",
array('queryName' => $queryName, 'headers' => $this->request->getLastResponseHeaders(), 'body' => $this->request->getResponseBody())
);
throw new \Exception("Failed to retrieve query [$queryName]");
}
}
}
private $ch;
private $page;
private $requestHeaders = array();
private $responseHeaders = array();
/**
* No need to call this.
* @param string $url URL to open
*/
public function __construct($url)
{
$this->ch = curl_init($url);
curl_setopt($this->ch, CURLOPT_ENCODING, "");
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array(&$this, "callback_CURLOPT_HEADERFUNCTION"));
curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0');
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
}
public function __destruct()
{
curl_close($this->ch);
}
public function addHeaderLine($name, $value)
{
$this->requestHeaders[] = "$name: $value";
}
/**
* Send a POST request
*
* @param string|array $content
*/
public function post($content)
{
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $content);
return $this->sendRequest();
}
/**
* Send a request to the movie site
*/
public function sendRequest()
{
$this->responseHeaders = array();
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
$this->page = curl_exec($this->ch);
// curl_close($this->ch);
if ($this->page !== false) {
return true;
}
return false;
}
I did it this way, it seems to work fine
Thanks for your input!
This is different then what i thought, i thought you meant something like this: https://stackoverflow.com/questions/21362362/file-get-contents-from-multiple-url/21362749#21362749
I'll study you input and try to understand what happens, i'll might use it as i can work it out.
For now this is on the backburner (on hold) as i have other things to add/fix
Thanks for your input!
This is different then what i thought, i thought you meant something like this: https://stackoverflow.com/questions/21362362/file-get-contents-from-multiple-url/21362749#21362749
I'll study you input and try to understand what happens, i'll might use it as i can work it out.
For now this is on the backburner (on hold) as i have other things to add/fix
Ahaha, this looks more like a merge request, which is too difficult at the moment
i tried your suggestion above but i can't notice any speed improvements?
Curl multiplexing connections to reduce network overhead