douxianwu2221 2016-09-08 21:16
浏览 61
已采纳

使用PHP获取Webservice数据

Please excuse me if i coun't explan well, i just need some examples, links or whatever you can provide me to learn how this works.

I'm trying to make this webservice work with PHP, also i have this as an example, that works with JS, but this webservice has some restrictions, like "I cannot consume at least has a different origin lenguage consumer".

This si the example.

$.ajax(
   {
       type:"POST",
       contentType:"application/json;charset=utf-8",
       url:"https://somewebsite.com/setsomedata",
       data:"{}",
       dataType:"json",
       async: false,
       success: function(msg) {
          a("#compInter",msg.d.data1); 
          a("#ventInter",msg.d.data2);
          a("#compAgencia",msg.d.data3);
          a("#ventAgencia",msg.d.data4);
      },
      error: function(textStatus, errorThrown, errorDetail){
          alert(errorDetail);
      }
   });

So, the problem is that i cannot use Jquery/Javascript to consume this data, because i need to use PHP to make this work.

So, i think i'm really lost in this case, that's why i would like to get some help, anything you can provide to make this work.

i'm trying to do this with PHP but i'm still gettint this error : [failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error ]

and this is my PHP code:

<?php

$result = json_decode(file_get_contents('https://somewebsite.com/setsomedata'),true);
print_r($result);

?>

Again, thank you so much.

  • 写回答

3条回答 默认 最新

  • dongmangwei3822 2016-09-11 15:55
    关注

    For all of you, if you're searching for something similar you can use try with this solutions that works for me.

        <?php
          $data = array("compraInternet" => " ", "ventaInternet" => " ", "compraAgencia" => " ", "ventaAgencia" => " ");
         $data_string = json_encode($data);
         $ch = curl_init('https://someurl.com/data');
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
         'Content-Type: application/json','Content-Length: ' . strlen($data_string)));
         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
         $result = curl_exec($ch);
         $json=json_decode($result,true);
         echo $json;
    
         curl_close($ch);
      ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵