使用cURL获取JSON

时间:2012-11-02 17:11:40

标签: php web-services curl

我正在尝试学习cURL以使用JSON Web服务。我在线尝试了几个教程,但似乎都没有帮助。

我想访问此网址:https://uat.petfirsthealthcare.com/pfh.service.webservices/coreservice.svc/json/GetColorList 并返回结果以用于另一个调用。但是当我尝试时,我没有得到任何回报。

我用过这个:

$request = 'https://uat.petfirsthealthcare.com/pfh.service.webservices/coreservice.svc/json/GetColorList';
curl_init($request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exe($session);
curl_close($session);
echo $response;

当你转到URL时,它返回JSON就好了。感谢您提供的任何帮助!

3 个答案:

答案 0 :(得分:2)

TESTED WORKS

<?php

$request = 'https://uat.petfirsthealthcare.com/pfh.service.webservices/coreservice.svc/json/GetColorList';
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $request);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_SSLVERSION,3); 
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($session, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$response = curl_exec($session);
curl_close($session);
var_dump($response);

答案 1 :(得分:1)

您是否初始化了$session

$session = curl_init($request);

这里有一个错字:

$response = curl_exec($session);

答案 2 :(得分:0)

我可以看到两个问题:

  1. 您未指定$session
  2. curl_exec(),而不是 curl_exe
  3. 更新代码

    <?php
    $request = 'https://uat.petfirsthealthcare.com/pfh.service.webservices/coreservice.svc/json/GetColorList';
    $session = curl_init($request);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);
    echo $response;