2017-05-17 3 views
1

저는 cURL 튜토리얼을 많이 읽었습니다. (저는 PHP를 사용하고 있습니다.) 항상 기본 코드가 있습니다. 구체적인 오류가없고 결과가 없습니다.WikiMedia API의 cURL HTTP 요청이 작동하지 않습니다.

Wikipedia에서 HTTP 요청을 만들고 결과를 JSON 형식으로 가져 오려고합니다.

$handle = curl_init(); 

$url = "http://fr.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json"; 

curl_setopt_array($handle, 
    array(
     CURLOPT_URL   => $url, 
     CURLOPT_RETURNTRANSFER => true 
) 
); 

$output = curl_exec($handle); 

if (!$output) { 
    exit('cURL Error: '.curl_error($handle)); 
} 

$result= json_decode($output,true); 
print_r($result); 

curl_close($handle); 

은 내가 잘못 알고 하시겠습니까 :

여기에 코드입니다.

답변

2

코드가 맞지만 PHP 컬을 사용할 때 위키피디아가 데이터를 다시 보내지 않는 것 같습니다 (어쩌면 일부 헤더 또는 다른 매개 변수가 작동하도록 설정해야 할 수도 있음). 모두 당신이 필요하지만 일부 데이터를 검색하는 경우

, 당신은 단순히 잘 작동하는 file_get_contents를 사용할 수 있습니다

$output = file_get_contents("http://fr.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json"); 
echo $output; 

편집 :

그냥 정보를, 나는 문제가 무엇인지 발견했다. 해당 URL에 curl -v를 실행하면 다음은 온다 :

* Trying 91.198.174.192... 
* Connected to fr.wikipedia.org (91.198.174.192) port 80 (#0) 
> GET /w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json HTTP/1.1 
> Host: fr.wikipedia.org 
> User-Agent: curl/7.47.0 
> Accept: */* 
> 
< HTTP/1.1 301 Moved Permanently 
< Date: Wed, 17 May 2017 13:54:31 GMT 
< Server: Varnish 
< X-Varnish: 852298595 
< X-Cache: cp3031 int 
< X-Cache-Status: int 
< Set-Cookie: WMF-Last-Access=17-May-2017;Path=/;HttpOnly;secure;Expires=Sun, 18 Jun 2017 12:00:00 GMT 
< Set-Cookie: WMF-Last-Access-Global=17-May-2017;Path=/;Domain=.wikipedia.org;HttpOnly;secure;Expires=Sun, 18 Jun 2017 12:00:00 GMT 
< X-Client-IP: 86.214.172.57 
< Location: https://fr.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json 
< Content-Length: 0 
< Connection: keep-alive 
< 
* Connection #0 to host fr.wikipedia.org left intact 

그래서 일어나는 것은이 직접 작업해야 https://fr.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=info&format=json을 요청하여, 그래서 실제 내용은 HTTPS URL이 아니라 HTTP에 있다는 것입니다.

이 경우 file_get_contents과 함께 작동하는 이유는이 경우 리디렉션이 자동으로 수행되기 때문입니다.

+0

https를 사용하면 "SSL 인증서 문제 : 로컬 발급자 인증서를 가져올 수 없습니다."라는 오류 메시지가 나타납니다. 이제는 file_get_contents를 그대로 사용하겠습니다. 감사 :) –

관련 문제