2017-02-24 25 views
0

私はPHPファイルでCURLコマンドを実行しようとしていますが、出力を見ることができません。以下は、実際のユーザー名/パスワード/ URLなしでわずかに変更されています。 私が出力を見たいのは、CURLコマンドが期待どおりに動作していることを確認することです(予想通りの結果が得られるようにbashで実行しています)。PHPがレスポンスを表示しないCURL

$ch = curl_init(); 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'); 
$headers = array(); 
$headers[] = 'Accept: application/json'; 
$headers[] = 'Content-Type: text/plain;charset=utf-8'; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERPWD, '$username:$password'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '$data'); 

// grab URL and pass it to the browser 
curl_exec($ch); 

私は確かにカールが私のPHPサーバーと一緒にインストールされていることを確認するには、以下の実行した、それはそうである:

function _is_curl_installed() { 
    if (in_array ('curl', get_loaded_extensions())) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

// Ouput text to user based on test 
if (_is_curl_installed()) { 
    echo "cURL is <span style=\"color:blue\">installed</span> on this server"; 
} else { 
    echo "cURL is NOT <span style=\"color:red\">installed</span> on this server"; 
} 

私が間違ってやっている何かがありますか?私は自分のbash上でcurlコマンドを実行してきた、と私は細かい反応を見ることができるよ:

curl -X POST --user $username:$password --header "Content-Type: text/plain;charset=utf-8" --header "Accept: application/json" --data-binary @Test.txt "http://www.example.com" 

答えて

1

この例を試してみてください。

<?php 
// SEND CURL POST DATA 
$jsonData = array(
    'user' => 'Username', 
    'pass' => 'Password' 
); 
//Encode the array into JSON. 
$jsonDataEncoded = json_encode($jsonData); 
//API Url 
$url = 'http://fxstar.eu/JSON/api.php'; 
//Initiate cURL. 
$ch = curl_init($url); 
//Tell cURL that we want to send a POST request. 
curl_setopt($ch, CURLOPT_POST, 1); 
//Attach our encoded JSON string to the POST fields. 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); 
//Set the content type to application/json 
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(Content-Type: text/plain;charset=utf-8); 
//Execute the request 
echo $result = curl_exec($ch); 
?> 

はJSからJSONを取得します。

<?php 
// GET JSON CONTENT FROM JS api.php 
$jsonStr = file_get_contents("php://input"); //read the HTTP body. 
$json = json_decode($jsonStr); 
?> 
+0

https://github.com/fxstar/PhpJsCss/blob/master/curl.php –

+0

私のために働いていません。何らかの仕掛けの例がありますか? – Adam

+0

http://stackoverflow.com/questions/31970541/php-curl-how-to-set-body-to-binary-data –

関連する問題