2013-11-26 49 views
0

這是我的php代碼,它沒有給出任何結果,請讓我知道如果我犯了什麼錯誤。curl用php不給任何結果

<?php 
$curl_handle=curl_init(); 
curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com  'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'"); 
$res=curl_exec($curl_handle); 
echo $res; 
curl_close($curl_handle); 
$json = json_decode($res, true); 
echo $json; 
print_r($json); 
?> 

回答

1

那是因爲你逝去的網址

curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com  'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'"); 
      --------------------------URL1--------------------------  ---------------------------------------URL2------------------------------------- 

通只是一個URL,如果你想使用多個網址,讓您使用loop和調用此方法cURL

編輯:

傳遞多個網址..

<?php 
$urls = array('http://example.com','http://example2.com'); 
foreach($urls as $url) 
{ 
$curl_handle=curl_init(); 
curl_setopt($curl_handle,CURLOPT_URL,$url); 
$res=curl_exec($curl_handle); 
echo $res; 
curl_close($curl_handle); 
$json = json_decode($res, true); 
echo $json; 
print_r($json); 
} 
?> 
+0

感謝您的答覆,但是,它是有兩個URL這是一個必須要捲曲執行的命令執行的curl命令 - e http://www.my-ajax-site.com'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs' – user2964771

+0

如果您想要做這樣,你可以在'exec'中傳遞相同的內容。看到這裏http://stackoverflow.com/questions/7572078/how-can-i-run-curl-as-shell-command-line-in-php或使用curl_multi http://php.net/manual/ en/function.curl-multi-init.php –

+0

謝謝,它的工作,但你也可以讓我知道如何做到這一點循環 – user2964771

0

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

resource curl_init ([ string $url = NULL ]) 

串$ URL = NULL

看看到的參數說明:

網址

如果提供,CURLOPT_URL選項將被設置爲它的價值。您可以使用curl_setopt()函數 手動設置此功能。

注意:如果設置了open_basedir,則cURL將禁用文件協議。

所以,這裏是CURLOPT_URL信息:

CURLOPT_URLURL獲取。當使用curl_init()初始化 會話時,也可以設置此值。

http://en.wikipedia.org/wiki/Uniform_resource_locator#Syntax

好,將只接受一個URL不是多個onces和存在的問題。

curl_setopt($curl_handle,CURLOPT_URL,"http://www.my-ajax-site.com  'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'"); 
//            URL 1             URL 2 

所以,這裏我們有2個網址。該參數是錯誤的,因此失敗。

我不知道你想做的事,但如果你想加入這兩個站點爲一個字符串,你可以做如下:

// Obtain the first URL data into $ObtainedDataFromSFirstCURL variable. 
string $variable = $ObtainedDataFromFirstCURL; 
// Obtain the second URL data into $ObtainedDataFromSecondCURL variable. 
$variable .= $ObtainedDataFromSecondCURL; 
// Do anything whit the variable. 
echo $variable; 

http://www.php.net/manual/en/language.operators.string.php

我希望它能幫助好歹。

0

在引用的curl命令行中,-e url參數設置HTTP引用鏈接,所以這就是您需要在PHP curl中模仿的東西。我發現在http://www.electrictoolbox.com/php-curl-http-referer/一個解決方案,我已經適應了你的URL:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, 'http://my-ajax-site.com'); 
$html = curl_exec($ch); 
+0

嘿,謝謝你的回覆,這爲我工作 – user2964771