0

ユーザー名とパスワードを要求する、保護された URL で HTTP GET 要求を発行しようとしています。これは、ブラウザから使用している場合は問題ありませんが、PHP を使用してそれを行う方法がわかりません。

私は2つの方法を使用してみました:

1) ここで提案されているように Curl を使用する: Make a HTTPS request through PHP and get response

2) ここで提案されているように file_get_contents を使用する: PHP から GET 要求を送信するには?

しかし、最初のものは私に何の返事も返してくれませんでした。そして2番目のものは私に次のエラーを与えました:

failed to open stream: HTTP request failed

そして、これはカールの私のコードです:

$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

file_get_contents の場合:

$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;

URL は、私がテストしている API の XML 応答を返します。誰かが私を正しい方向に向けることができますか?

ありがとう!

4

1 に答える 1

0

ユーザー名とパスワードを送信する必要性に焦点を当てている場合、それが主な問題であると思われるため、これを試してください

$ch = curl_init();

$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation

$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better 
// than nothing during debug
curl_close($ch);
于 2012-10-08T23:52:35.280 に答える