Skip to content

Instantly share code, notes, and snippets.

@cmnstmntmn
Last active April 16, 2020 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmnstmntmn/8f01d6b15832da94737bbe80c6e3fbf8 to your computer and use it in GitHub Desktop.
Save cmnstmntmn/8f01d6b15832da94737bbe80c6e3fbf8 to your computer and use it in GitHub Desktop.
custom_datasource.php
public static function prepareGateway(&$gateway)
{
$user = [
"user" => [
"email" => "email",
"password" => "pass"
]
];
$data_string = json_encode($user);
$gateway->setopt('POST', true);
$gateway->setopt('RETURNHEADERS', true);
$gateway->setopt('POSTFIELDS', $data_string);
$gateway->setopt('HTTPHEADER',array('Content-Type: application/json;charset=UTF-8'));
}
/**
* Given a URL, Format and Timeout, this function will initalise
* Symphony's Gateway class to retrieve the contents of the URL.
*
* @param string $url
* @param string $format
* @param integer $timeout
* @return array
*/
public static function fetch($url, $format, $timeout)
{
$ch = new Gateway;
$ch->init($url);
$ch->setopt('TIMEOUT', $timeout);
// Set the approtiate Accept: headers depending on the format of the URL.
if ($transformer = self::getTransformer($format)) {
$accepts = $transformer->accepts();
$ch->setopt('HTTPHEADER', array('Accept: ' . $accepts));
}
// use static here to allow late static binding
// see http://php.net/manual/en/language.oop5.late-static-bindings.php
static::prepareGateway($ch);
$response = $ch->exec();
list($header, $body) = explode("\r\n\r\n", $response, 2);
$data = array(
"payload" => json_decode($body)
);
foreach (explode("\r\n", $header) as $i => $line) {
if ($i === 0)
$data["headers"]['http_code'] = $line;
else
{
list($key, $value) = explode(': ', $line);
$data["headers"][$key] = $value;
}
};
return array(
json_encode($data),
$ch->getInfoLast(),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment