API tutorial help

Post here is you are having problems with any of the tutorials.
Post Reply
jjmpsp
Posts: 2
Joined: Fri Oct 21, 2011 5:58 pm

API tutorial help

Post by jjmpsp »

I'm having problems getting the api_interfece.php code to work :(
<?php

class foodclass{

	const API_URL = 'http://localhost/food/api.php';
	
        // internal function for sending post data
        private static function send_post_data($data)
        {
                $url = parse_url(self::API_URL);
                $boundary = md5(microtime(true));
               
                $post = '';
               
                foreach($data as $name => $value)
                {
                        $post .= "--{$boundary}\r\n";
                        $post .= "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n";
                        $post .= "{$value}\r\n";
                }
               
                $post .= "--{$boundary}--\r\n";
               
                if(isset($url['query']))
                {
                        $head = "POST {$url['path']}?{$url['query']} HTTP/1.1\r\n";
                }
                else
                {
                        $head = "POST {$url['path']} HTTP/1.1\r\n";
                }
               
                $head .= "Host: {$url['host']}\r\n";
                $head .= "Content-Type: multipart/form-data; boundary=\"{$boundary}\"\r\n";
                $head .= "Content-Length: " . strlen($post) . "\r\n";
                $head .= "Connection: close \r\n\r\n";
                               
                $socket = fsockopen($url['host'], ((isset($url['port'])) ? $url['port'] : 80));
               
                fwrite($socket, "{$head}{$post}");
               
                return (end(explode("\r\n\r\n", stream_get_contents($socket))));
        }
	   
	   
		// Gets the user_id for the reference id given
		public static function lookup($ref)
		{
				return json_decode(self::send_post_data(array('x' => $ref)), true);
		}
}

foodclass::lookup('x');
?>
For some reason, I get this error:
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\food\api_interface.php on line 42
Does anyone know what is wrong? I can get data with the api.php file without any problems...

Thanks in advance, oh and great tutorial btw ;)

Edit:
I've just done some further testing.

If I change the code at the end to:

print_r (explode("\r\n\r\n", stream_get_contents($socket)));

I get the correct output, so something is telling me that the end() function is causing this problem!
I'll post back if I manage to solve the problem.

Edit 2:
Problem solved! :D

It was the end() function causing the problem!

Fix was:
$parts =  explode("\r\n\r\n", stream_get_contents($socket));
				return $parts[1];
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API tutorial help

Post by jacek »

The problem is that PHP does not want you to use function arguments as parameters to other functions.

So instead of
return (end(explode("\r\n\r\n", stream_get_contents($socket))));
it wants
$data = stream_get_contents($socket);
$data = explode("\r\n\r\n", $data);
return end($data);
which looks worse.

You can also disable these messages by setting your error_reporting setting to E_ALL
error_reporting(E_ALL);
Image
Post Reply