2016-11-06 164 views
0

我想調用一個API並POST一些數據。我得到如下回應:php curl返回空字符串

string '' (length=0). 

這裏是代碼:

$subscriberInfo = [ 
    $subscriberEmail => $email, 
    $subscriberJobType => $jobType, 
    $subscriberLocation => $location 
]; 

$ch = curl_init('https://URLTOEXTERNALSITE'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: MY API KEY HERE', 'Content-Type: text/x-php-serialization' 
    )); 

// execute! 
$response = curl_exec($ch); 

// close the connection, release resources 
curl_close($ch); 

var_dump($response); 
die(); 

爲什麼我只得到返回一個空字符串?

編輯

這是一個從API文檔

$ curl -X POST EXTERNAL/URL/GOES/HERE -u $API_KEY: \ 

--form [email protected] \ 
--form create_time='2012-05-03 09:45:23' \ 
--form search='Legal Assistant' \ 
--form location='Houston, TX' \ 
--form content-type=application/json 
+1

http://php.net/manual/en/function.curl-error.php – Deep

+0

Shoudnt你有POSTFILEDS $ subscriberInfo而不是$張貼? – Mihai

+0

@Deep - 沒有記錄錯誤 –

回答

-1

你忘了打電話給

curl_exec($ CH)一個JSON例子;

$響應= curl_exec($ CH);

// execute! 
    $response = curl_exec($ch); 

    curl_exec($ch); 

    // close the connection, release resources 
    curl_close($ch); 
+1

發出此請求。在這裏調用'$ response = curl_exec($ ch);'。無需兩次撥打電話 –

+0

@PaulDessert可能會讓你捲曲肌肉)) – Deep

+0

嘗試我的答案中的代碼。 –