Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

php - Request method GETS Zero value

Hi i am trying to get the request method when the URL is requested from my localhost. I have done URL rewriting so that http://localhost/API2/products.php?product_id=1 becomes http://localhost/API2/products.php/products/1 as a result $request_method=$_SERVER["REQUEST_METHOD"]; should hold GET as the method. But rather it gets zero every time.

my.htaccess file holds these url rules:

      RewriteEngine On # Turn on the rewriting engine
      RewriteRule ^products/?$ products.php [NC,L]
      RewriteRule ^products/([0-9]+)/?$ products.php?product_id=$1 [NC,L]

My curl commands in learn.php are:

   <?php
    $url = 'http://localhost/API2/products';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response_json = curl_exec($ch);
    curl_close($ch);
    $response=json_decode($response_json, true);

   ?>

So when i run my curl command which is learn.php neither it presents me any data because $request_method=$_SERVER["REQUEST_METHOD"] the request method is holding zero value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add that to disable MultiViews:

Options -MultiViews

The Apache docs on mod_negotiation, describes what the Multiviews Option does, when enabled:

If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.

Use:

# disable products -> products.php
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteRule ^products/?$ products.php [NC,L]
RewriteRule ^products/([0-9]+)/?$ products.php?product_id=$1 [NC,L]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...