dongmu1390 2014-10-17 18:03 采纳率: 100%
浏览 40
已采纳

将数据发送到站点以获取带有php的Json

I'm having an issue, the thing is that I was reading a json file with jquery, and it was working well, but now I have to make it read it in php, but the thing is that I was using some data to obtain the parts of the json with:

dataString = "id=" + id + "&todos=" + false;
$.ajax({
type: "POST",
url: "http://www.url.com/example",
data: dataString,
dataType: "json",

success: function( data, textStatus, jqXHR) {

And with this I had no problem, since I was sending the data to the site so it can give me the info that I wanted, but I have no clue of how to do it in php, i was trying with

$str = file_get_contents('http://www.url.com/example.json');
$json = json_decode($str, true); 
var_dump($str);

But of course the site its returning me nothing since I'm not sending the data

I hope there's a way. Thanks!

  • 写回答

2条回答 默认 最新

  • dtvjl64442 2014-10-17 18:39
    关注

    You should use curl or fsockopen if the first is not present and cannot be enabled (pretty rare case).

    Here how you do that with curl

    <?php
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'id' => $id,
        'todos' => false
    ));
    $json = json_decode( curl_exec($ch) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度