2016-06-01 63 views
0

PHP +捲曲問題的資源ID#2 curl_init:PHP +捲曲問題的資源ID#2 curl_init

 
$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON 
     $ch = curl_init($url); 
     echo $ch; //write Resource id # 2 
     if($ch) 
     { 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE); 
      $json = curl_exec($ch); 
      $json = json_decode($json); 
     } else { 
      echo 'nothing'; 
      } 

我在做什麼錯?

+0

請谷歌並按照步驟。我相信你一定能弄明白。 – Vickrant

回答

1

curl_init返回一個cURL句柄成功,錯誤爲FALSE。 因此,echo $ch;將返回類似於資源ID#2的內容。

http://php.net/manual/en/function.curl-init.php

你必須嘗試這樣的事情

$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE); 
$json = curl_exec($ch); 
$json = json_decode($json); 
curl_close($ch); 

if(empty($json)){ 
    echo 'nothing'; 
} 
1

如果你不使用SSL的主機上,所以你應該繞過SSL驗證

<?php 
    $url = "https://example.com:4433/deviceservice/authorize?login=query"; 
    $ch = curl_init($url); 
    echo $ch; //write Resource id # 2 
    if($ch) 
    { 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
     $json = curl_exec($ch); 
     $json = json_decode($json); 
    } else { 
     echo 'nothing'; 
    } 
2

嘗試使用curl_error($ ch)和echo來診斷錯誤

$ch= curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
)                  
);  

$response = curl_exec($ch); 
$err_status = curl_error($ch); 
echo $err_status; 
curl_close($ch);