2016-05-11 84 views
1

我嘗試使用curl調用API時遇到問題。我的PHP代碼如下所示:如何在PHP中使用CURL調用REST API?

<html> 
<head> 
<body> 
<span style="z-index:50;font-size:0.9em;"> 
<img src="https://theysaidso.com/branding/theysaidso.png" height="20" width="20" alt="theysaidso.com"/> 
<a href="https://theysaidso.com" title="Powered by quotes from theysaidso.com" style="color: #9fcc25; margin-left: 4px; vertical-align: middle;"> 
theysaidso.com</a></span> 
<?php 
     $service_url = 'http://quotes.rest/qod.json'; 
     $curl = curl_init($service_url); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_POST, false); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     $curl_response = curl_exec($curl); 
     curl_close($curl); 
     $json_objekat=json_decode($curl_response); 
     echo $json_objekat->contents->quotes->quote.'<br>'; 
     echo $json_objekat->contents->quotes->author.'<br>';  
?> 
</body> 
</head> 

這個代碼在WAMP的服務器上我的根目錄另存爲PHP文件。我使用WAMP服務器。它寫在這個網站http://quotes.rest/qod.json您必須也插入這個html。當我打開WAMP這個PHP頁面它顯示警報:

Notice: Undefined property: stdClass::$contents in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18

Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18

Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18

Notice: Undefined property: stdClass::$contents in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19

Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19

Notice: Trying to get property of non-object in C:\wamp\www\IzdavackaKuca\javniServis.php on line 19

JSON此鏈接http://quotes.rest/qod.json上看起來是這樣的:

{ 
    "success": { 
     "total": 1 
    }, 
    "contents": { 
     "quotes": [ 
      { 
       "quote": "A loser doesn't know what he'll do if he loses, but talks about what he'll do if he wins, and a winner doesn't talk about what he'll do if he wins, but knows what he'll do if he loses.", 
       "length": "184", 
       "author": null, 
       "tags": [ 
        "failure", 
        "inspire", 
        "knowledge", 
        "winning" 
       ], 
       "category": "inspire", 
       "date": "2016-05-11", 
       "title": "Inspiring Quote of the day", 
       "background":  "https://theysaidso.com/img/bgs/man_on_the_mountain.jpg", 
       "id": "KhjRMynny89MKxcGkEKF_QeF" 
      } 
     ] 
    } 
} 

當我更改最後兩行PHP這樣的:

echo $json_objekat['contents']['quotes']['quote'].'<br>'; 
    echo $json_objekat['contents']['quotes']['author'].'<br>'; 

它提醒:

Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\IzdavackaKuca\javniServis.php on line 18

我真的不`噸知道該怎麼稱呼這個服務器上,當我代碼:

<?php 
    $defaults = array(
     CURLOPT_URL    => 'http://quotes.rest/qod.json', 
     CURLOPT_POST   => false, 
     CURLOPT_HEADER   => false, 
     CURLOPT_SSL_VERIFYPEER => false, 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_SSL_VERIFYHOST => false, 
    ); 
    $curl    = curl_init(); 
    curl_setopt_array($curl, $defaults); 
    $curl_response  = curl_exec($curl); 
    $json_objekat  = json_decode($curl_response); 

    // DUMP THE CURL-ERROR INFORMATION: 
    var_dump(curl_error($curl)); 
    curl_close($curl); 
?> 

它提醒:

string '' (length=0)

請幫幫忙!

回答

0

這個問題是因爲你正在接受報價和作者不正確。的print_r($json_objekat) 輸出說:

contents is stdClass Object
quotes is array
again quotes is having 0 as index which is again stdClass Object

因此,嘗試訪問報價和作者如下:

$json_objekat->contents->quotes[0]->quote 

$json_objekat->contents->quotes[0]->author 
+0

是的,這個工作,非常感謝你! – Svetlana

0

我假定這是你打算做什麼:

<?php 
     $service_url  = 'http://quotes.rest/qod.json'; 
     $curl   = curl_init($service_url); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_POST, false); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     $curl_response = curl_exec($curl); 
     curl_close($curl); 
     $json_objekat = json_decode($curl_response); 
     $quotes   = $json_objekat->contents->quotes; 

     foreach($quotes as $intKey=>$objQuote){ 
      echo $objQuote->title  . '<br>'; 
      echo $objQuote->author  . '<br>'; 
      echo $objQuote->quote  . '<br>'; 
      echo $objQuote->background . '<br>'; 
     } 
    ?> 

但請注意,使用相同的IP地址,每小時的請求數不能超過10個。 因此,一旦您完成10個請求,您將被阻止,您必須等待另一個1小時才能繼續處理您的請求。 希望這有助於......

+0

這也適用於Suyog的回答。謝謝你這麼爛! – Svetlana

+0

$引號= $ json_objekat-> contents->之後的括號就是多餘的。這是錯誤,我知道... – Svetlana

0
<?php 
$conn = mysqli_connect("localhost","root","","mydatabase"); 

if(!$conn){ 
    die("Connection failed: " . mysqli_connect_error()); 
} 

if(!function_exists("curl_init")) die("cURL extension is not installed"); 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "http://localhost/ketan/all_user.php"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl); 
curl_close($curl); 
echo $result; 
//print_r(json_decode($result));//To Decode Result 
?> 

Output

Output

all_user.php

<?php 
$conn = mysqli_connect("localhost","root","","mydatabase"); 

if(!$conn){ 
    die("Connection failed: " . mysqli_connect_error()); 
} 

$result = mysqli_query($conn,"SELECT * FROM tblUser"); 

$data = array(); 

while ($row = mysqli_fetch_array($result)) { 
    /*$result = array(); 
    $result['userId'] = $row['userId']; 
    $result['firstName'] = $row['firstName']; 
    $result['lastName'] = $row['lastName'];*/ 
    array_push($data, array('userId'=> $row['userId'],'firstName'=> $row['firstName'], 'lastName'=>$row['lastName'], 
     'email'=>$row['email'], 'phoneNumber'=>$row['phoneNumber'], 'userImage'=>"user_images/".$row['userImage'])); 
} 


$return['status'] = true; 
$return['message'] = 'Success'; 
$return['data'] = $data; 
header('Content-Type: application/json'); 
echo json_encode($return, JSON_PRETTY_PRINT); 
$conn->close(); 

?>