I am new to PHP and was trying to build RestFull service in PHP for my Mobile App. I googled and found Create a REST API with PHP Article best as per recommendations by many people on Stackoverflow and other places.
But while reading this article I arranged this code like below and was unable to figure out that the given last two functions, where to place them?
PHP is new language for me .. so please help me.
class RestUtils
{
public static function processRequest()
{
// get our verb
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$return_obj = new RestRequest();
// we'll store our data here
$data = array();
switch ($request_method)
{
// gets are easy...
case 'get':
$data = $_GET;
break;
// so are posts
case 'post':
$data = $_POST;
break;
// here's the tricky bit...
case 'put':
// basically, we read a string from PHP's special input location,
// and then parse it out into an array via parse_str... per the PHP docs:
// Parses str as if it were the query string passed via a URL and sets
// variables in the current scope.
parse_str(file_get_contents('php://input'), $put_vars);
$data = $put_vars;
break;
}
// store the method
$return_obj->setMethod($request_method);
// set the raw data, so we can access it if needed (there may be
// other pieces to your requests)
$return_obj->setRequestVars($data);
if(isset($data['data']))
{
// translate the JSON to an Object for use however you want
$return_obj->setData(json_decode($data['data']));
}
return $return_obj;
}
public static function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status);
// set the status
header($status_header);
// set the content type
header('Content-type: ' . $content_type);
// pages with body are easy
if($body != '')
{
// send the body
echo $body;
exit;
}
// we need to create the body if none is passed
else
{
// create some body messages
$message = '';
// this is purely optional, but makes the pages a little nicer to read
// for your users. Since you won't likely send a lot of different status codes,
// this also shouldn't be too ponderous to maintain
switch($status)
{
case 401:
$message = 'You must be authorized to view this page.';
break;
case 404:
$message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
break;
case 500:
$message = 'The server encountered an error processing your request.';
break;
case 501:
$message = 'The requested method is not implemented.';
break;
}
// servers don't always have a signature turned on (this is an apache directive "ServerSignature On")
$signature = ($_SERVER['SERVER_SIGNATURE'] == '') ? $_SERVER['SERVER_SOFTWARE'] . ' Server at ' . $_SERVER['SERVER_NAME'] . ' Port ' . $_SERVER['SERVER_PORT'] : $_SERVER['SERVER_SIGNATURE'];
// this should be templatized in a real-world solution
$body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>' . $status . ' ' . RestUtils::getStatusCodeMessage($status) . '</title>
</head>
<body>
<h1>' . RestUtils::getStatusCodeMessage($status) . '</h1>
<p>' . $message . '</p>
<hr />
<address>' . $signature . '</address>
</body>
</html>';
echo $body;
exit;
}
}
public static function getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
}
class RestRequest
{
private $request_vars;
private $data;
private $http_accept;
private $method;
public function __construct()
{
$this->request_vars = array();
$this->data = '';
$this->http_accept = (strpos($_SERVER['HTTP_ACCEPT'], 'json')) ? 'json' : 'xml';
$this->method = 'get';
}
public function setData($data)
{
$this->data = $data;
}
public function setMethod($method)
{
$this->method = $method;
}
public function setRequestVars($request_vars)
{
$this->request_vars = $request_vars;
}
public function getData()
{
return $this->data;
}
public function getMethod()
{
return $this->method;
}
public function getHttpAccept()
{
return $this->http_accept;
}
public function getRequestVars()
{
return $this->request_vars;
}
}
///// Code srgments Which I am unable to put at right position///////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
$data = RestUtils::processRequest();
switch($data->getMethod)
{
case 'get':
// retrieve a list of users
break;
case 'post':
$user = new User();
$user->setFirstName($data->getData()->first_name); // just for example, this should be done cleaner
// and so on...
$user->save();
break;
// etc, etc, etc...
}
/////////////////////////////////
switch($data->getMethod)
{
// this is a request for all users, not one in particular
case 'get':
$user_list = getUserList(); // assume this returns an array
if($data->getHttpAccept == 'json')
{
RestUtils::sendResponse(200, json_encode($user_list), 'application/json');
}
else if ($data->getHttpAccept == 'xml')
{
// using the XML_SERIALIZER Pear Package
$options = array
(
'indent' => ' ',
'addDecl' => false,
'rootName' => $fc->getAction(),
XML_SERIALIZER_OPTION_RETURN_RESULT => true
);
$serializer = new XML_Serializer($options);
RestUtils::sendResponse(200, $serializer->serialize($user_list), 'application/xml');
}
break;
// new user create
case 'post':
$user = new User();
$user->setFirstName($data->getData()->first_name); // just for example, this should be done cleaner
// and so on...
$user->save();
// just send the new ID as the body
RestUtils::sendResponse(201, $user->getId());
break;
}
/////////////////////////////////////////////////////////////////////////////////////
Check out this link,
we can use CodeIgniter to create a RESTful API for your existing web applications, and demonstrate how to interact with your own API or other RESTful web-services, such as Facebook and Twitter.
Working with Restful Services in CodeIgniter
May this helpful you.
Related
When I want to update the endpoint, I got the error:
ErrorException: Creating default object from empty value in file /var/www/html/app/Http/Controllers/CasesController.php on line 208
below the code:
public function accept(Request $request)
{
$id_case = $request->id_case;
$case = Cases::find($id_case);
$maxAgent = 0;
switch ($id_case[0]) {
case 'A':
$maxAgent = 1;
break;
case 'B':
$maxAgent = 3;
break;
case 'C':
$maxAgent = 5;
break;
default:
break;
}
// check the agent receive the case
$accepted_agent = AgentInCase::where('id_case', $id_case)->where('id_agent', auth()->user()->id_user)->get();
if ($accepted_agent) {
$message = 'Agent has accepted the request';
return $this->sendResponse($accepted_agent, $message, 200);
}
// check the max agent in case
$agent_in_case = AgentInCase::where('id_case', $id_case)->get();
if (count($agent_in_case) < $maxAgent) {
$agent = Agent::find(auth()->user()->id_user);
$agent->is_in_case = true;
$agent->save();
$agent = AgentInCase::create([
'id_case' => $id_case,
'id_agent' => auth()->user()->id_user
]);
//send FCM to customer that request confirmed
$message = [
'title' => 'accept_agent',
'body' => 'New Agent Come To Help ',
'payment' => 'Rp 15.000'
];
$this->sendNotificationFirebase('customer', $case->id_customer, $message, $agent);
$message = 'Successfully accepted request';
return $this->sendResponse($case, $message, 200);
}
$message = 'Failed to accept request';
$errors = 'Case has been handled by another agent';
return $this->sendError($message, 400, $errors);
}
The error in line:
$agent->is_in_case = true;
I'm trying to solve this error and I just get one reference:
Creating default object from empty value in PHP?.
But, I can't yet to solving this.
Could you help me to solve this? What I must do?
For every request that you want to select by an ID, you should catch and error if requested id is not exists .
In my opinion clean way to do that in laravel is using validation .
For example if you want to find a user by its ID validation should be something like :
'ID' => 'exists:users,ID',
First Id is in your request and second one is your column name in database.
I am trying to build an email server for my domains... What I'm doing now is receiving emails through SES and storing them in a S3 bucket, then when a user access the inbox it fetches the new emails and store them in my EC2 instance database.
Though it works I'm not completely satisfied with this solution, does anyone know any other/better ways to work this receiving-storing-accessing problem?
Thanks in advance.
You can try to install postfix (http://www.postfix.org/) and dovecot (http://www.dovecot.org/) on your EC2 instance.
I've resolved this and posted the answer on another question I'd made here.
But I'll repost it here anyway:
So what I did was storing the email received in an S3 bucket, than notifying my api that a new email has arrived (sending the file name). Finally read from S3, parsed, stored and deleted from S3, inside my api.
SES rules:
Lambda notifying function:
Note that the name of the S3 file created by the first rule is the same as the messages id, hence 'fileName': event.Records[0].ses.mail.messageId.
'use strict';
exports.handler = (event, context, callback) => {
var http = require('http');
var data = JSON.stringify({
'fileName': event.Records[0].ses.mail.messageId,
});
var options = {
host: 'my.host',
port: '80',
path: '/my/path',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': data.length
}
};
var req = http.request(options, function(res) {
var msg = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
msg += chunk;
});
res.on('end', function() {
console.log(JSON.parse(msg));
context.succeed();
});
});
req.write(data);
req.end();
};
Api function (PHP - Laravel):
Note that I'm using an email parser that's based on Plancake Email Parser (link here) with some changes of my own and if needed I'll edit to show the source.
public function process_incoming_email(Request $request)
{
$current_time = Carbon::now()->setTimezone('Brazil/East'); // ALL TIMEZONES: http://us.php.net/manual/en/timezones.others.php
try
{
if ($request->has('fileName')
{
$file_name = $request->input('fileName');
// GET CREDENTIALS AND AUTHENTICATE
$credentials = CredentialProvider::env();
$s3 = new S3Client([
'version' => 'latest',
'region' => 'my-region',
'credentials' => $credentials
]);
// FECTH S3 OBJECT
$object = $s3->GetObject(['Bucket' => 'my-bucket', 'Key' => $file_name]);
$body = $object['Body']->getContents();
// PARSE S3 OBJECT
$parser = new EmailParser($body);
$receivers = ['to' => $parser->getTo(), 'cc' => $parser->getCc()];
$from = $parser->getFrom();
$body_plain = $parser->getPlainBody();
$body_html = $parser->getHTMLBody();
$subject = $parser->getSubject();
$error_message;
// PROCESS EACH RECEIVER
foreach ($receivers as $type => $type_receivers)
{
foreach ($type_receivers as $receiver)
{
// PROCESS DOMAIN-MATCHING RECEIVERS
if(preg_match("/#(.*)/", $receiver['email'], $matches) && $matches[1] == self::HOST)
{
// INSERT NEW EMAIL
$inserted = DB::table('my-emails')->insert([
// ...
]);
}
}
}
// ADD ERROR LOG IF PARSER COULD NOT FIND EMAILS
if($email_count == 0)
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Could not parse received email or find a suitable user receiving email.') . ' File: ' . $file_name]
);
}
// DELETE OBJECT FROM S3 IF INSERTED
else if(count($emails) == $email_count)
{
$s3->deleteObject(['Bucket' => 'my-bucket', 'Key' => $file_name]);
// RETURN SUCCESSFUL JSON RESPONSE
return Response::json(['success' => true, 'receivedAt' => $current_time, 'message' => 'Email successfully received and processed.']);
}
// ADD ERROR LOG IF NOT INSERTED
else
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Inserted ' . count($emails) . ' out of ' . $email_count . ' parsed records.') . ' File: ' . $file_name]
);
}
}
else
{
// ERROR: NO fileName FIELD IN RESPONSE
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Incorrect request input format.') . ' Input: ' . json_encode($request->all())]
);
}
}
// ERROR TREATMENT
catch(Exception $ex)
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'An exception occurred while processing an incoming email.') . ' Details: ' . $ex->getMessage()]
);
}
// RETURN FAILURE JSON RESPONSE
return Response::json(['success' => false, 'receivedAt' => $current_time, 'message' => $error_message]);
}
I want to update a customer in PrestaShop using a webservice. I search for the customer based on his email and company name and then update with new details. But when I use filters to get the customer details and then update i receive the following error and im not sure how to resolve this issue.
Error 90 - id is required when modifying a resource
Code
$email=$_POST['email'];
$oldEmail=$_POST['oldEmail'];
$company=$_POST['company'];
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array(
'resource' => 'customers',
'display' => 'full',
'filter[email]'=>'e.siona#example.gr',//['.$oldEmail.']',
'filter[company]'=>'euro'//['.$company.']'
);
$xml = $webService->get($opt);
// Retrieve resource elements in a variable (table)
$resources = $xml->children()->children();
//$opt['id'] = 17;
//echo $resources->id;
}
catch (PrestaShopWebserviceException $e){
// Here we are dealing with errors
$trace = $e->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error';
}
$resources->email = 'e.siona43#example.gr';
// Resource definition
$opt = array('resource' => 'customers');
//XML file definition
$opt['putXml'] = $xml->asXML();
$opt['id'] = 'How can I put here the id retrieved from previous call?';
// Calling asXML() returns a string corresponding to the file
$xml = $webService->edit($opt);
// Second : We update the data and send it to the web service
There was an error in official github examples.
Try this corrected version of your snippet:
$email=$_POST['email'];
$oldEmail=$_POST['oldEmail'];
$company=$_POST['company'];
try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
// Filters must be an array (I'll suggest this method)
$filter = array(
'email' => $oldEmail,
'company' => 'ACME'
);
// I guess it's useless to use company because the e-mail it's a KEY in the database, so the correspondence 'should' be univocal.
$opt = array(
'resource' => 'customers',
'display' => 'full',
'filter' => $filter
);
$xml = $webService->get($opt);
// Retrieve resource elements in a variable (table)
$resources = $xml->children()->children();
}
catch (PrestaShopWebserviceException $e){
// Here we are dealing with errors
$trace = $e->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error';
}
// Here we retrieve the ID
$customer_id = $resources->customer->id;
// Here we update the e-mail
$resources->customer->email = $email; // E.g. 'e.siona43#example.gr';
// And call the web service
try
{
$opt = array('resource' => 'customers');
$opt['putXml'] = $xml->children()->asXML(); // This is correct if you have one customer in the XML. The GitHub examples it's incomplete and you get the error 90.
$opt['id'] = $customer_id;
$xml = $webService->edit($opt);
// if WebService don't throw an exception the action worked well and we don't show the following message
echo "Successfully updated.";
}
catch (PrestaShopWebserviceException $ex)
{
// Here we are dealing with errors
$trace = $ex->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$ex->getMessage();
}
Hope this helps :)
function drupal_http_request($url, array $options = array()) {
// Allow an alternate HTTP client library to replace Drupal's default
// implementation.
$override_function = variable_get('drupal_http_request_function', FALSE);
if (!empty($override_function) && function_exists($override_function)) {
return $override_function($url, $options);
}
$result = new stdClass();
// Parse the URL and make sure we can handle the schema.
$uri = #parse_url($url);
if ($uri == FALSE) {
$result->error = 'unable to parse URL';
$result->code = -1001;
return $result;
}
if (!isset($uri['scheme'])) {
$result->error = 'missing schema';
$result->code = -1002;
return $result;
}
timer_start(__FUNCTION__);
// Merge the default options.
$options += array(
'headers' => array(),
'method' => 'GET',
'data' => NULL,
'max_redirects' => 3,
'timeout' => 30.0,
'context' => NULL,
);
// Merge the default headers.
$options['headers'] += array(
'User-Agent' => 'Drupal (+http://drupal.org/)',
);
// stream_socket_client() requires timeout to be a float.
$options['timeout'] = (float) $options['timeout'];
// Use a proxy if one is defined and the host is not on the excluded list.
$proxy_server = variable_get('proxy_server', '');
if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
// Set the scheme so we open a socket to the proxy server.
$uri['scheme'] = 'proxy';
// Set the path to be the full URL.
$uri['path'] = $url;
// Since the URL is passed as the path, we won't use the parsed query.
unset($uri['query']);
// Add in username and password to Proxy-Authorization header if needed.
if ($proxy_username = variable_get('proxy_username', '')) {
$proxy_password = variable_get('proxy_password', '');
$options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
}
// Some proxies reject requests with any User-Agent headers, while others
// require a specific one.
$proxy_user_agent = variable_get('proxy_user_agent', '');
// The default value matches neither condition.
if ($proxy_user_agent === NULL) {
unset($options['headers']['User-Agent']);
}
elseif ($proxy_user_agent) {
$options['headers']['User-Agent'] = $proxy_user_agent;
}
}
switch ($uri['scheme']) {
case 'proxy':
// Make the socket connection to a proxy server.
$socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080);
// The Host header still needs to match the real request.
$options['headers']['Host'] = $uri['host'];
$options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
break;
case 'http':
case 'feed':
$port = isset($uri['port']) ? $uri['port'] : 80;
$socket = 'tcp://' . $uri['host'] . ':' . $port;
// RFC 2616: "non-standard ports MUST, default ports MAY be included".
// We don't add the standard port to prevent from breaking rewrite rules
// checking the host that do not take into account the port number.
$options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
break;
case 'https':
// Note: Only works when PHP is compiled with OpenSSL support.
$port = isset($uri['port']) ? $uri['port'] : 443;
$socket = 'ssl://' . $uri['host'] . ':' . $port;
$options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
break;
default:
$result->error = 'invalid schema ' . $uri['scheme'];
$result->code = -1003;
return $result;
}
if (empty($options['context'])) {
$fp = #stream_socket_client($socket, $errno, $errstr, $options['timeout']);
}
else {
// Create a stream with context. Allows verification of a SSL certificate.
$fp = #stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
}
var_dump($fp);
// Make sure the socket opened properly.
if (!$fp) {
// When a network error occurs, we use a negative number so it does not
// clash with the HTTP status codes.
$result->code = -$errno;
$result->error = trim($errstr) ? trim($errstr) : t('Error opening socket #socket', array('#socket' => $socket));
print_r($result);
exit();
// Mark that this request failed. This will trigger a check of the web
// server's ability to make outgoing HTTP requests the next time that
// requirements checking is performed.
// See system_requirements().
variable_set('drupal_http_request_fails', TRUE);
return $result;
}
// Construct the path to act on.
$path = isset($uri['path']) ? $uri['path'] : '/';
if (isset($uri['query'])) {
$path .= '?' . $uri['query'];
}
// Only add Content-Length if we actually have any content or if it is a POST
// or PUT request. Some non-standard servers get confused by Content-Length in
// at least HEAD/GET requests, and Squid always requires Content-Length in
// POST/PUT requests.
$content_length = strlen($options['data']);
if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
$options['headers']['Content-Length'] = $content_length;
}
// If the server URL has a user then attempt to use basic authentication.
if (isset($uri['user'])) {
$options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ':'));
}
// If the database prefix is being used by SimpleTest to run the tests in a copied
// database then set the user-agent header to the database prefix so that any
// calls to other Drupal pages will run the SimpleTest prefixed database. The
// user-agent is used to ensure that multiple testing sessions running at the
// same time won't interfere with each other as they would if the database
// prefix were stored statically in a file or database variable.
$test_info = &$GLOBALS['drupal_test_info'];
if (!empty($test_info['test_run_id'])) {
$options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
}
$request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
foreach ($options['headers'] as $name => $value) {
$request .= $name . ': ' . trim($value) . "\r\n";
}
$request .= "\r\n" . $options['data'];
$result->request = $request;
// Calculate how much time is left of the original timeout value.
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
if ($timeout > 0) {
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
fwrite($fp, $request);
}
// Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
// and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
// instead must invoke stream_get_meta_data() each iteration.
$info = stream_get_meta_data($fp);
$alive = !$info['eof'] && !$info['timed_out'];
$response = '';
while ($alive) {
// Calculate how much time is left of the original timeout value.
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
if ($timeout <= 0) {
$info['timed_out'] = TRUE;
break;
}
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
$chunk = fread($fp, 1024);
$response .= $chunk;
$info = stream_get_meta_data($fp);
$alive = !$info['eof'] && !$info['timed_out'] && $chunk;
}
fclose($fp);
if ($info['timed_out']) {
$result->code = HTTP_REQUEST_TIMEOUT;
$result->error = 'request timed out';
return $result;
}
// Parse response headers from the response body.
// Be tolerant of malformed HTTP responses that separate header and body with
// \n\n or \r\r instead of \r\n\r\n.
list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$response = preg_split("/\r\n|\n|\r/", $response);
// Parse the response status line.
$response_status_array = _drupal_parse_response_status(trim(array_shift($response)));
$result->protocol = $response_status_array['http_version'];
$result->status_message = $response_status_array['reason_phrase'];
$code = $response_status_array['response_code'];
$result->headers = array();
// Parse the response headers.
while ($line = trim(array_shift($response))) {
list($name, $value) = explode(':', $line, 2);
$name = strtolower($name);
if (isset($result->headers[$name]) && $name == 'set-cookie') {
// RFC 2109: the Set-Cookie response header comprises the token Set-
// Cookie:, followed by a comma-separated list of one or more cookies.
$result->headers[$name] .= ',' . trim($value);
}
else {
$result->headers[$name] = trim($value);
}
}
$responses = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
);
// RFC 2616 states that all unknown HTTP codes must be treated the same as the
// base code in their class.
if (!isset($responses[$code])) {
$code = floor($code / 100) * 100;
}
$result->code = $code;
switch ($code) {
case 200: // OK
case 304: // Not modified
break;
case 301: // Moved permanently
case 302: // Moved temporarily
case 307: // Moved temporarily
$location = $result->headers['location'];
$options['timeout'] -= timer_read(__FUNCTION__) / 1000;
if ($options['timeout'] <= 0) {
$result->code = HTTP_REQUEST_TIMEOUT;
$result->error = 'request timed out';
}
elseif ($options['max_redirects']) {
// Redirect to the new location.
$options['max_redirects']--;
$result = drupal_http_request($location, $options);
$result->redirect_code = $code;
}
if (!isset($result->redirect_url)) {
$result->redirect_url = $location;
}
break;
default:
$result->error = $result->status_message;
}
return $result;
}
I'm getting the below mentioned message by doing var_dump of $fp as mentioned above on my VM machine hosted with Ubuntu 14.04
stdClass Object
(
[code] => 0
[error] => php_network_getaddresses: getaddrinfo failed: Name or service not known
)
When I'm implementing the same thing on my localhost which is XAMPP based in Windows 7 I'm getting this:
Resource id #8
Due to this I'm unable to use the drupal_http_request
As per your suggestion I've tried dns_get_record()
$dns_get_record = dns_get_record("www.google.com");
print_r($dns_get_record);
and got this as the output:
Array
(
[0] => Array
(
[host] => www.google.com
[class] => IN
[ttl] => 243
[type] => A
[ip] => 216.58.220.4
)
[1] => Array
(
[host] => www.google.com
[class] => IN
[ttl] => 257
[type] => AAAA
[ipv6] => 2404:6800:4009:805::2004
)
)
I've also checked the stream_socket_client()
var_dump(stream_socket_client());
and it returned me bool(false)
Since you've not put any proper error handling in the code, we can't tell which function is being executed. You haven't provided the parameters you are passing, so we can't tell what the cause is, but the most likely issue is that you are trying to establish a network connection and the host where the code is running is unable to resolve the hostname inside $socket.
You can very this with dns_get_record() or speak to your hosting provider.
Api Version 0.6.2
I was following this answer:
Post large video to youtube via google php client api v3
I'm getting
Failed to start the resumable upload
500 Internal Server Error - Google_Exception
Stack Trace
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/service/Google_MediaFileUpload.php at line 260 -+
if (200 == $code && true == $location) {
return $location;
}
throw new Google_Exception("Failed to start the resumable upload");
}
}
at Google_MediaFileUpload ->getResumeUri (object(Google_HttpRequest))
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/service/Google_MediaFileUpload.php at line 214 -+
public function nextChunk(Google_HttpRequest $req, $chunk=false) {
if (false == $this->resumeUri) {
$this->resumeUri = $this->getResumeUri($req);
}
if (false == $chunk) {
at Google_MediaFileUpload ->nextChunk (object(Google_HttpRequest), '')
in /home/darko/NetBeansProjects/shekortet/src/Dalu/MediaBundle/Controller/EncodeController.php at line 284 -+
while (!$status && !feof($handle))
{
$chunk = fread($handle, $chunkSizeBytes);
$uploadStatus = $media->nextChunk($result, $chunk);
var_dump($uploadStatus);
}
When trying to post a nonresumable upload I'm getting a 400 Bad Request error.
Error Message
Error calling POST https://www.googleapis.com/youtube/v3/videos?part=status%2Csnippet&key=replacedkeystring: (400) Bad Request
Stack Trace
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/io/Google_REST.php at line 66 -+
$err .= ": ($code) $body";
}
throw new Google_ServiceException($err, $code, null, $decoded['error']['errors']);
}
// Only attempt to decode the response, if the response code wasn't (204) 'no content'
at Google_REST ::decodeHttpResponse (object(Google_HttpRequest))
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/io/Google_REST.php at line 36 -+
*/
static public function execute(Google_HttpRequest $req) {
$httpRequest = Google_Client::$io->makeRequest($req);
$decodedResponse = self::decodeHttpResponse($httpRequest);
$ret = isset($decodedResponse['data'])
? $decodedResponse['data'] : $decodedResponse;
return $ret;
at Google_REST ::execute (object(Google_HttpRequest))
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/service/Google_ServiceResource.php at line 186 -+
return $httpRequest;
}
return Google_REST::execute($httpRequest);
}
public function useObjects() {
at Google_ServiceResource ->__call ('insert', array(array('part' => 'status,snippet', 'postBody' => object(Google_Video), 'mediaUpload' => object(Google_MediaFileUpload))))
in /home/darko/NetBeansProjects/shekortet/vendor/google/google-api-php-client/src/contrib/Google_YouTubeService.php at line 789 -+
public function insert($part, Google_Video $postBody, $optParams = array()) {
$params = array('part' => $part, 'postBody' => $postBody);
$params = array_merge($params, $optParams);
$data = $this->__call('insert', array($params));
if ($this->useObjects()) {
return new Google_Video($data);
} else {
at Google_VideosServiceResource ->insert ('status,snippet', object(Google_Video), array('mediaUpload' => object(Google_MediaFileUpload)))
in /home/darko/NetBeansProjects/shekortet/src/Dalu/MediaBundle/Controller/EncodeController.php at line 277 -+
$media = new \Google_MediaFileUpload('video/x-matroska', null, false, $chunkSizeBytes);
$media->setFileSize(filesize($filename));
$result = $youtube->videos->insert("status,snippet", $gvideo, ['mediaUpload' => $media]);
$status = false;
$handle = fopen($filename, "rb");
I have authenticated a few hours ago. I saw that the token lifetime was 3600 (1h).
This is also done locally
Could this be the reason or what could be the reason?
Any help appreciated.