简体   繁体   中英

Insert Variable into file_get_contents from HTML form

I've used the answer given here ( how do i insert a variable? from a form into a file_get_contents link ) however, It is still not working for me.

But this is not working, I do not know why?

You need to place/assign your POST array and variable first, not after:
(Kind of like putting the horse after the wagon, as it were) .

<?php
$code = $_POST['search'];
$json = file_get_contents("http://myurl.com/user=". $code);
$obj = json_decode($json);
?>

Because as it stands, you'd be getting an "Undefined index/variable..." notice, had you been checking for errors.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM