Didn"t forge 2011-04-15 20:32 采纳率: 83.3%
浏览 12

不存储POST数据(json)

I have this bit of javascript:

var jsonString = "some string of json";
$.post('proxy.php', { data : jsonString }, function(response) {
    var print = response;
    alert(print);

and this bit of PHP (in proxy.php):


       $json = $_POST['json'];

       //set POST variables, THIS IS WHERE I WANT TO POST TO!
       $url = 'http://my.site.com/post';

       //open connection
       $ch = curl_init();

       //set the url, number of POST vars, POST data
       curl_setopt($ch,CURLOPT_URL, $url);
       curl_setopt($ch,CURLOPT_POST, 1);
       curl_setopt($ch,CURLOPT_POSTFIELDS, "data=" . urlencode($json));

       //execute post (the result will be something like {"result":1,"error":"","pic":"43248234af832048","code":"234920348239048"})
       $result = curl_exec($ch);
       $response = json_decode($result);

       $imageHref = 'http://my.site.com/render?picid=' . $response['picid'];

       //close connection   
       curl_close($ch);

       echo $imageHref;

I am trying to post data to an external site using a proxy. From there, I append the picid that the site responds with and append it to the URL to get the image URL.

Am I missing something here? I am not getting anything in response and it seems like my data is not even being posted (when I try echo $json after the first line in proxy.php, I get an empty string). Why am I not able to echo the JSON? Is my implementation correct?

Thanks!

  • 写回答

2条回答 默认 最新

  • ?yb? 2011-04-15 20:35
    关注

    In your Javascript code, you are using this :

    { data : jsonString }
    

    So, from your PHP code, should you not be reading from $_POST['data'], instead of $_POST['json'] ?


    If necessary, you can use var_dump() to see what's in $_POST :

    var_dump($_POST);
    


    Edit after the comment : if you are getting a JSON result such as this :

    {"result":1,"error":"","pic":"43248234af832048","code":"234920348239048"}
    

    This is a JSON object -- which means, after decoding it, you should access it as an object in PHP :

    $response = json_decode($result);
    echo $response->pic;
    

    Note : I don't see a picid element in that object -- maybe you should instead use pic ?


    Here too, though, you might want to use var_dump(), to see how your data looks like :

    var_dump($response);
    
    评论

报告相同问题?