0

I have a html page which sends a get request to php. This is the code snippet in the php file

<?php
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>'GET',
      )
    );

    $context = stream_context_create($opts);
    //echo("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token={$_GET['token']}&agencyName={$_GET['agency']}&stopName={$_GET['stopname']}");

    // Open the file using the HTTP headers set above
    $file = file_get_contents("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token={$_GET['token']}&agencyName={$_GET['agency']}&stopName={$_GET['stopname']}", false, $context);
    //$file = file_get_contents("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=123-456-789&agencyName=SF-MUNI&stopName=The%20Embarcadero%20and%20Folsom%20St", false, $context);
    echo(json_encode(simplexml_load_string($file)));
?>

Developer Console Output :

Warning: file_get_contents(http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?token=123-456-789&amp;amp;agencyName=BART&amp;amp;stopName=Powell St. (SF)): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD_REQUEST

As you can see from the developer console output, in the url request sent there are BART&amp;amp;stopName amp;amp; being inserted in the url which I'm not doing. The request fails due to this. Any solution around this?

1

3 Answers 3

1

Try the below code, this will make sure that you're stuff is properly URI encoded.

$params = [
    'token' => $_GET['token'],
    'agencyName' => $_GET['agency'],
    'stopName' => $_GET['stopname']
];

$file = file_get_contents(sprintf("http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?%s", http_build_query($params));
echo(json_encode(simplexml_load_string($file)));
0
0

Try this one:

$data = array('token'=>$_GET['token'],
          'stopname'=>$_GET['stopname'],
          'agency'=>$_GET['agency'],
);
$url = "http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?".$data;
$file = file_get_contents($url, false, $context);
echo(json_encode(simplexml_load_string($file)));

Note: I have modified my answer base on your comment.

3
  • @PrasadShinde Checkout modified answer May 3, 2015 at 16:40
  • It still did no work. Some issue with the concatenation but the request still contained extra params May 3, 2015 at 17:05
  • Still extra param? I got this "<transitServiceError>Parameter Required</transitServiceError>". additional param require. May 3, 2015 at 17:09
0

You can try this way to clean out the url:

<?php
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>'GET',
      )
    );



    $context = stream_context_create($opts);
    $url= "http://services.my511.org/Transit2.0/GetNextDeparturesByStopName.aspx?";
    $query =  array(
        "token" =>$_GET['token'],
        "agencyName"=>$_GET['agency'],
        "stopName"=>$_GET['stopname']
        );

    $url = $url.http_build_query($query);
    $url = rawurldecode($url);
    print_r($url);

    $file = file_get_contents($url, false, $context);
    echo(json_encode(simplexml_load_string($file)));
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.