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条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan