API

Post here is you are having problems with any of the tutorials.
Post Reply
10016932
Posts: 2
Joined: Sun Feb 24, 2013 5:46 pm

API

Post by 10016932 »

Having trouble with your API, I just get Strict standards: Only variables should be passed by reference in C:\wamp\www\test\api_interface.inc.php on line 35 BAD REQUEST

api_interface.inc.php code
<?php
class url_shortener 
{
	const API_URL = "http://localhost:80/shortner/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.0\r\n";
                } else {
                        $head = "POST = {$url['path']} HTTP/1.0\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}");
               
                echo end(explode("\r\n\r\n", stream_get_contents($socket)));
	}
	//Gets the URL for the given key.
	public static function lookup ($key)
	{
		self::send_post_data(array("lookup" => $key));
		//self::send_post_data(0);
	}
	//Shortens the URL.
	public static function shorten ($url)
	{
		
	}
}
?>

HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

Array
(
    [scheme] => http
    [host] => localhost
    [port] => 80
    [path] => /shortner/api.php
)
POST = /shortner/api.php HTTP/1.0
Host: localhost
Content-Type: multipart/form-data; boundary="ec1ec0a5e31a6720e7292de48ce3a805"
Content-Length: 126
Connection: close

--ec1ec0a5e31a6720e7292de48ce3a805
Content-Disposition: form-data; name="lookup"

0
--ec1ec0a5e31a6720e7292de48ce3a805--
<br />
<font size='1'><table class='xdebug-error xe-strict-standards' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Strict standards: Only variables should be passed by reference in C:\wamp\www\test\api_interface.inc.php on line <i>37</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>245128</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\test\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>260976</td><td bgcolor='#eeeeec'>url_shortener::lookup(  )</td><td title='C:\wamp\www\test\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>14</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>261304</td><td bgcolor='#eeeeec'>url_shortener::send_post_data(  )</td><td title='C:\wamp\www\test\api_interface.inc.php' bgcolor='#eeeeec'>..\api_interface.inc.php<b>:</b>42</td></tr>
</table></font>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
</body>
</html>

Any help would be greatly appreciated
Last edited by Helx on Sun Feb 24, 2013 9:05 pm, edited 2 times in total.
Reason: Code tags
10016932
Posts: 2
Joined: Sun Feb 24, 2013 5:46 pm

Re: API

Post by 10016932 »

Found my error - Great tut thanks you :)
Post Reply