1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】cakePHPリモート API 通信(HTTP

Posted at

CakePHP には、リクエストの実行に基本HTTP クライアントを利用して、ウェブサービスや、リモート API と通信するという方法があります。

HTTPクライアントで基本の通信方法を紹介します。

post/get/delete

httpPost.php
// リクエストURL
$url = "http://localhost/app/DummyService/httpTest.php";

// HTTPヘッダ設定
$option = array (
	"headers" => [
		'AuthID'   => '123456789',
		'AuthPass' => '1Qaz2wSxc'
	]
);

// HTTPリクエストデータ
$requestData = [
	'NUM'       => '8888',
	'NICKNAME'  => 'Chelsea',
	'FAVOURITE' => 'PHP',
	'LANGUAGE'  -> mb_convert_encoding("日本語", "SJIS", "UTF-8");
];

// POST通知(deleteにも同様)
try {
	$client = new Client ();
	$response = $client->post ( $url, $requestData, $option );
	$client = NULL;
	$httpHeader = $response->headers();
	$httpStatus = $response->statusCode ();
	$httpBody = $response->body ();
	$httpBody = json_decode ( $httpBody, 'TRUE' );
} catch ( \Exception $e ) {
	var_dump( $e->getMessage () );
	return FALSE;
}

// http statusチェック(200番以外)
if (! $response->isOk ()) {
	return FALSE;
}

---I Love PHP (。・ω・。)ノ?

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?