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 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"