2012-11-13 29 views
1

我有以下的curl命令從Windows命令行完美的作品:使用PHP做一個CURL文章?

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d "{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"[email protected]\"}" http://localhost:8080/users 

所以JSON爲這個職位是:

{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"[email protected]\"} 

我嘗試使用下面的PHP代碼來執行它:

$ch = curl_init(); // initialize curl handle 
$user_agent = $_SERVER['HTTP_USER_AGENT']; 
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 5s 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
if (!empty($request)) { 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields 
} 

if($port==443) { 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
} 

$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch); 
$error = curl_errno($ch); 
curl_close($ch); 

if ($error) { 
    $error_codes = $this->_get_curl_error_codes(); 
    echo 
    'Sorry, but we are currently not able to connect to the database. Error code '.$error.': '.$error_codes[$error]; 
    die(); 
} 

我用這個代碼如下:

$url = 'http://localhost/users'; 
$port = 8080; 
$request = array(); 
$request['name']  = 'Name'; 
$request['password'] = '1234'; 
$request['email']  = '[email protected]'; 

JSON是創建像這樣:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields 

我的問題是,如果我在命令行中運行它,它的工作原理,但如果我運行使用上面的代碼,它不工作。任何想法爲什麼?

從控制檯
+0

curl_exec取消註釋此波之後「捲曲的錯誤:」。 curl_error($ CH); – Fivell

+0

這也意味着$ this - > _ get_curl_error_codes(); ? 你腳本不是類的一部分,所以這會引發一個錯誤 – Fivell

回答

1

curl命令使用PUT,您使用POST

嘗試下選擇

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$json); 

例如http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

,如果它不能正常工作,還添加頁眉

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($json))); 

還有一件事: 有一個$this->_get_curl_error_codes();但我沒有在你的代碼

+0

現在我得到「請求實體太大」 – coderama

+0

等待,我認爲strlen($ json)部分將解決該問題。測試... – coderama

+0

結束於「現在允許的方法」錯誤,但似乎我現在正處於正確的道路上。我完全錯過了「PUT」部分...... – coderama

0

注意到這種方法此代碼可以幫助你

function get($url, $params=array()) { 

    $url = $url.'?'.http_build_query($params, '', '&'); 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    $response = curl_exec($ch); 

    curl_close($ch); 

    return $response; 

}