URL working with & but not &

99 views Asked by At

I have this link which works fine.

http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E

I want to do the get the content of this URL but the issue is the file_get_contents content convert & to &

http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E

Which doesn't work.

$url = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E";
$content = file_get_contents($url);
echo $content;

It generates error.

$url = "http://www.google.com";
$content = file_get_contents($url);
echo $content;

It works fine.

1

There are 1 answers

3
drew010 On BEST ANSWER

Without setting a User-Agent for the request, I get a 403 forbidden error.

This code works:

<?php

$url = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E";

$opts = array(
    'http' => array('header' => 'User-Agent: test'),
);

$ctx = stream_context_create($opts);

$content = file_get_contents($url, false, $ctx);
var_dump($content);

Looks like the site by default disallows the PHP user agent so you will need to specify one using the stream context or use another library like cURL to fetch the content.

This is a common problem when trying to use file_get_contents to fetch remote URLs.