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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)