0

I am having a problem with PHP's file_get_contents command.

$url = "http://api.rememberthemilk.com/services/rest/".$format.$auth_token.$filter."&api_sig=".$md5.$apikey.$method;

$content = file_get_contents($url);

$array = json_decode($content, true);
$taskname = $array['rsp']['tasks']['list']['taskseries']['name'];
$duedate = $array['rsp']['tasks']['list']['taskseries']['task']['due'];

($format, $auth_token, $filter, $md5, $apikey, $method are already defined in the script)

When I try to run this code this error is returned:

[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad request for line 101

line 101 = $content, = file_get_contents($url);

How to fix this? Thanks!!!

Community
  • 1
  • 1
01jayss
  • 1,400
  • 6
  • 19
  • 28
  • 2
    There's nothing to fix in `file_get_contents`. The URL you are trying to read is not correct. – Jon Mar 06 '11 at 16:43
  • 1
    Please show the full URL you are trying to read (sans your auth token of course) – Pekka Mar 06 '11 at 16:44
  • It might also be the request that is invalid (wrong parameters, you seemingly concatenate a bit much), or the PHP user_agent that is blocked. – mario Mar 06 '11 at 16:45
  • you might want to check the value of allow_url_fopen. Useful information also found as notes on the php docs for this function. – Andreas Mar 06 '11 at 16:55
  • here's the url: `http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList` (I replaced the actual auth_token, api_sig and api_key) – 01jayss Mar 06 '11 at 17:29
  • when creating URLs, always encode the query string, use urlencode() and urldecode(). – Kumar Mar 06 '11 at 17:48

2 Answers2

2

This url does not look great.

http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&­filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList

Encode the tokens as follows:

$filter = 'filter='.urlencode( 'dueWithin:"3 days of today"' );

Use urlencode().

tacone
  • 11,371
  • 8
  • 43
  • 60
0

Try printing the URL after concatenating the variables. Then paste the URL into the address bar of your browser and see what comes back. Because it's a web service call, your browser might not know what to do with the response. In that case you might get additional information using the command-line user agent "curl", e.g.

curl -v 'http://some-url'

curl is built in to Macs and other *nix machines and is also available for Windows.

inkfist
  • 162
  • 2