javascript - URL Encoding forward slashes in PHP - Stack Overflow

i have a page in PHP where i'm trying to connect to an API and get information on motorcycles, the

i have a page in PHP where i'm trying to connect to an API and get information on motorcycles, the problem here is that i use an internal route to call the API via CURL and this route is accessible as follows

/manufacturer/BETA/year/2016/category/M/model/MINI%20CROSS%2010%2F10/versions

I encode this and then i try to send it through to the API where it gets decoded and the JSON response sent back, however the problem is that for some reason it's not even getting to the route's method whenever i have any forward slashes, regardless of me encoding them or not...

I'm really running out of options here, what exactly is the problem here? Shouldn't URI Encoding fix these sorts of issues? Why is PHP still treating %2F as if it were a forward slash? And is there any way to make this work?

EDIT

In my javascript i'm getting the text for the Model via jQuery and encoding it as follows

var model    = $(".model-list li.active").text();
model = encodeURI(model);

Then i have a laravel route that takes in the encoded string from the Javascript call and returns it to a PHP function

public function versionsByYear($manufacturer, $year, $category, $model)
{
    $manufacturer = str_replace(" ", "%20", $manufacturer);
    $model        = str_replace(" ", "%20", $model);

    return $this->connect('manufacturer/'.$manufacturer.'/year/'.$year.'/category/'.$category.'/model/'.$model.'/versions');
}

Which in turn links to another one that makes a CURL request to the external API

public function connect($method)
{

    $url    = $this->apiUrl . '/' . $method;

    $ch     = curl_init();

    curl_setopt_array($ch, array(
        CURLOPT_URL             => $url,
        CURLOPT_HTTPHEADER      => array('Token: ' . $this->token),
        CURLOPT_RETURNTRANSFER  => true
    ));

    $out = curl_exec($ch);

    curl_close($ch);

    return $out;

}

The API itself is not the problem as it works when i pass it the exact same encoded model names that fail on my application. the Application simply returns an empty string, having failed to find the method defined in the route pletely, possibly due to interpreting the encoded %20 as a forward slash?

i have a page in PHP where i'm trying to connect to an API and get information on motorcycles, the problem here is that i use an internal route to call the API via CURL and this route is accessible as follows

http://mywebpage.dev/manufacturer/BETA/year/2016/category/M/model/MINI%20CROSS%2010%2F10/versions

I encode this and then i try to send it through to the API where it gets decoded and the JSON response sent back, however the problem is that for some reason it's not even getting to the route's method whenever i have any forward slashes, regardless of me encoding them or not...

I'm really running out of options here, what exactly is the problem here? Shouldn't URI Encoding fix these sorts of issues? Why is PHP still treating %2F as if it were a forward slash? And is there any way to make this work?

EDIT

In my javascript i'm getting the text for the Model via jQuery and encoding it as follows

var model    = $(".model-list li.active").text();
model = encodeURI(model);

Then i have a laravel route that takes in the encoded string from the Javascript call and returns it to a PHP function

public function versionsByYear($manufacturer, $year, $category, $model)
{
    $manufacturer = str_replace(" ", "%20", $manufacturer);
    $model        = str_replace(" ", "%20", $model);

    return $this->connect('manufacturer/'.$manufacturer.'/year/'.$year.'/category/'.$category.'/model/'.$model.'/versions');
}

Which in turn links to another one that makes a CURL request to the external API

public function connect($method)
{

    $url    = $this->apiUrl . '/' . $method;

    $ch     = curl_init();

    curl_setopt_array($ch, array(
        CURLOPT_URL             => $url,
        CURLOPT_HTTPHEADER      => array('Token: ' . $this->token),
        CURLOPT_RETURNTRANSFER  => true
    ));

    $out = curl_exec($ch);

    curl_close($ch);

    return $out;

}

The API itself is not the problem as it works when i pass it the exact same encoded model names that fail on my application. the Application simply returns an empty string, having failed to find the method defined in the route pletely, possibly due to interpreting the encoded %20 as a forward slash?

Share Improve this question edited Jun 27, 2016 at 15:57 João Serra asked Jun 27, 2016 at 15:38 João SerraJoão Serra 2534 silver badges14 bronze badges 2
  • The link you provided is already URL-encoded, if you encode it again it will bee messy. – apokryfos Commented Jun 27, 2016 at 15:46
  • No no the link is the result of the initial javascript encoding and call to the backend method, that link is an internal route that makes a CURL request, the problem is that despite the encoding the method fails to trigger – João Serra Commented Jun 27, 2016 at 15:52
Add a ment  | 

2 Answers 2

Reset to default 6

The encoded slashes are probably decoded as normal path separators by your webserver before they are passed to PHP. For example, Apache has a specific setting to allow these: AllowEncodedSlashes On.

You can use this to keep Apache from parsing the encoded slashes.

<VirtualHost 127.0.0.1:80>
  ServerName site.example.
  AllowEncodedSlashes On
  # ...
</VirtualHost>

PHP Documentation has a really good solution for what I think is happening here:

<?php
function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
    return str_replace($entities, $replacements, urlencode($string));
}
?>

http://php/manual/en/function.urlencode.php

It deals with the special characters.

**It may be worth posting your code here or clarifying what's happening if this isn't what you're looking for.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742284671a4415111.html

相关推荐

  • javascript - URL Encoding forward slashes in PHP - Stack Overflow

    i have a page in PHP where i'm trying to connect to an API and get information on motorcycles, the

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信