API tutorial help
Posted: Sat Oct 22, 2011 10:35 am
I'm having problems getting the api_interfece.php code to work 
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!
It was the end() function causing the problem!
Fix was:
<?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 42Does 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!
It was the end() function causing the problem!
Fix was:
$parts = explode("\r\n\r\n", stream_get_contents($socket));
return $parts[1];