نظرة على واجهة برمجة تطبيقات HTTP WordPress: مثال عملي على wp_remote_get
() translation by (you can also view the original English article)
في المقالة الأخيرة في هذه السلسلة ، ألقينا نظرة على وظائف PHP المتاحة لتقديم الطلبات عن بُعد.
على وجه التحديد ، قمنا باستعراض :
file_get_contents
cURL
وناقشنا أيضًا وظيفة wp_remote_get
.
في هذه المقالة ، سنعمل على تشغيل wp_remote_get
. هذه الوظيفة جزء من HTTP API - للاستخدام العملي باستخدامها لاسترداد الأمرين التاليين:
- عدد المتابعين لدينا على تويتر
- أحدث تغريدة لدينا
الشيء الجميل هو أننا لن نحتاج إلى استخدام أي آليات OAuth أو آليات المصادقة ، وسنحتاج فقط إلى الاستفادة من ردود Twitter ووظائف JSON الخاصة بـ PHP.
لذلك في هذه المقالة ، سنلقي نظرة عملية على كيفية القيام بذلك بالضبط ، ثم سننهي السلسلة التي تراجع كل المعلومات التي تُرجعها wp_remote_get
حتى نعرف كيفية التعامل معها بشكل صحيح في العمل المستقبلي .
إعداد دليل البرنامج المساعد
كما هو الحال مع جميع الإضافات ، فإن أول ما نحتاج إلى فعله هو إنشاء دليل في دليل wp-content / plugins. لأغراض هذا العرض التوضيحي ، سوف نتصل بالتطبيق الإضافي على Twitter plugin.
وبالتالي ، دعونا تسمية دليل البرنامج المساعد twitter-demo وملف البرنامج المساعد المرتبط twitter-demo.php.



بعد ذلك ، نحتاج إلى المضي قدمًا وإخراج رأس المكون الإضافي حتى يتمكن WordPress من اكتشاف ملف المكون الإضافي ، لذلك دعونا نفعل ذلك الآن.
الخروج من المساعد
أولاً ، ضع الكود التالي في رأس ملف twitter-demo.php:
1 |
<?php
|
2 |
/* Plugin Name: Twitter Demo
|
3 |
* Plugin URI: http://example.com/twitter-demo/
|
4 |
* Description: Retrieves the number of followers and latest Tweet from your Twitter account.
|
5 |
* Version: 1.0.0
|
6 |
* Author: Tom McFarlin
|
7 |
* Author URI: http://tommcfarlin.com/
|
8 |
* License: GPL-2.0+
|
9 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
10 |
*/
|
لاحظ أننا لن نقوم بتدويل هذا البرنامج المساعد. لقد غطينا هذا الموضوع في منشور آخر وهو أمر خارج عن نطاق ما نحاول القيام به في هذه المقالة.
في هذه المرحلة ، يجب أن تكون قادرًا على رؤية المكوّن الإضافي معروضًا في لوحة معلومات البرنامج المساعد لتثبيت WordPress. يمكنك تفعيله. ومع ذلك ، فإنه لن يفعل أي شيء في الواقع.
على الأقل ليس بعد.
جلب المساعد إلى الحياة
كما هو الحال مع بقية الإضافات التجريبية التي أنشرها هنا ، أعتقد أنه من المهم أولاً تحديد الخطوط العريضة لما سيفعله البرنامج المساعد قبل أن نصل إلى الترميز.
على هذا النحو ، إليك ما يمكن أن نتوقعه:
- في أسفل كل مشاركة ، سنقوم بعرض إشعار صغير نصه: لدي X عدد المتابعين على Twitter. كانت آخر تغريدة لي هي Y.
- سنحرص على القيام بذلك فقط في صفحة المنشورات الفردية بحيث لا يتم عرضها على الفهرس أو صفحات الأرشيف الرئيسية.
من المؤكد أنه من المثير للاشمئزاز عرض هذا في أسفل المنشور ، لكن تذكر أن الغرض من هذا المكون الإضافي هو توضيح كيفية استخدام wp_remote_get
، وكيفية تحليل استجابة من Twitter ، وكيفية عرضها.
نحن أقل اهتمامًا بكثير بمكان عرض المحتوى.
لذلك ، دعونا نمضي قدمًا ونبطل الفصل الذي سيوفر هذه الوظيفة.
1. كعب الروتين خارج فئة العرض التوضيحي لتويتر
قبل القيام بأي شيء ، دعنا نكشف عن الفصل الذي سنستخدمه لتقديم الطلب إلى Twitter. لقد قمت بتضمين كافة التعليمات البرمجية أدناه بالإضافة إلى وثائق لكل سمة وطريقة.
1 |
<?php
|
2 |
/**
|
3 |
* Plugin Name: Twitter Demo
|
4 |
* Plugin URI: http://tommcfarlin.com/twitter-demo/
|
5 |
* Description: Retrieves the number of followers and latest Tweet from your Twitter account.
|
6 |
* Version: 1.0.0
|
7 |
* Author: Tom McFarlin
|
8 |
* Author URI: http://tommcfarlin.com/
|
9 |
* License: GPL-2.0+
|
10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
11 |
*/
|
12 |
|
13 |
class Twitter_Demo { |
14 |
|
15 |
/**
|
16 |
* Instance of this class.
|
17 |
*
|
18 |
* @var Twitter_Demo
|
19 |
*/
|
20 |
private static $instance; |
21 |
|
22 |
/**
|
23 |
* Initializes the plugin so that the Twitter information is appended to the end of a single post.
|
24 |
* Note that this constructor relies on the Singleton Pattern
|
25 |
*
|
26 |
* @access private
|
27 |
*/
|
28 |
private function __construct() { |
29 |
|
30 |
} // end constructor |
31 |
|
32 |
/**
|
33 |
* Creates an instance of this class
|
34 |
*
|
35 |
* @access public
|
36 |
* @return Twitter_Demo An instance of this class
|
37 |
*/
|
38 |
public function get_instance() { |
39 |
|
40 |
} // end get_instance |
41 |
|
42 |
/**
|
43 |
* Appends a message to the bottom of a single post including the number of followers and the last Tweet.
|
44 |
*
|
45 |
* @access public
|
46 |
* @param $content The post content
|
47 |
* @return $content The post content with the Twitter information appended to it.
|
48 |
*/
|
49 |
public function display_twitter_information( $content ) { |
50 |
|
51 |
} // end display_twitter_information |
52 |
|
53 |
/**
|
54 |
* Attempts to request the specified user's JSON feed from Twitter
|
55 |
*
|
56 |
* @access public
|
57 |
* @param $username The username for the JSON feed we're attempting to retrieve
|
58 |
* @return $json The user's JSON feed or null of the request failed
|
59 |
*/
|
60 |
private function make_twitter_request( $username ) { |
61 |
|
62 |
} // end make_twitter_request |
63 |
|
64 |
/**
|
65 |
* Retrieves the number of followers from the JSON feed
|
66 |
*
|
67 |
* @access private
|
68 |
* @param $json The user's JSON feed
|
69 |
* @return The number of followers for the user. -1 if the JSON data isn't properly set.
|
70 |
*/
|
71 |
private function get_follower_count( $json ) { |
72 |
|
73 |
} // end get_follower_count |
74 |
|
75 |
/**
|
76 |
* Retrieves the last tweet from the user's JSON feed
|
77 |
*
|
78 |
* @access private
|
79 |
* @param $json The user's JSON feed
|
80 |
* @return The last tweet from the user's feed. '[ No tweet found. ]' if the data isn't properly set.
|
81 |
*/
|
82 |
private function get_last_tweet( $json ) { |
83 |
|
84 |
} // end get_last_tweet |
85 |
|
86 |
} // end class |
87 |
|
88 |
// Trigger the plugin
|
89 |
Twitter_Demo::get_instance(); |
لاحظ أننا سنقوم بملء بقية هذه الأساليب كما نذهب ، وسأحصل أيضًا على شفرة المصدر الكاملة للمكون الإضافي في نهاية المقالة.
قبل أن نذهب إلى أبعد من ذلك ، أود أن أشير إلى أننا سنستخدم نموذج Singleton لهذا البرنامج المساعد. لقد قمنا بتغطية نمط التصميم هذا في مقال سابق ، وعلى الرغم من أن ميزاته تقع خارج نطاق هذه المقالة ، إلا أنني أوصي بقراءة المقال المرتبط للتأكد من أنك تعرف تمامًا سبب قيامنا بإعداد المكون الإضافي الخاص بنا بهذه الطريقة.
بعد ذلك ، دعونا نلقي نظرة على الوظائف التي وضعناها حتى نعرف بالضبط أين نتجه:
- المنشئ هو المكان الذي سنقوم بإضافة إجراء لإلحاق معلومات Twitter الخاصة بنا إلى منشور واحد
- سيتم استخدام
display_twitter_information
لعرض الرسالة في أسفل المنشور -
make_twitter_request
سيطلب فعليًا ويعيد البيانات من Twitter (أو لاغٍ إذا فشل الطلب) - سيعيد
get_follower_count
عدد المتابعين للمستخدم المحدد (أو -1 في حالة وجود مشكلة) -
get_last_tweet
سيعيد آخر تغريدة من المستخدم أو رسالة في حالة فشل المكون الإضافي
واضح بشكل كافي؟ في هذه المرحلة ، دعونا نعمل على طلب المعلومات من Twitter حتى نتمكن من معالجتها.
2. طلب البيانات من تويتر
أولاً ، دعنا نشر وظيفة make_twitter_request
بالكود التالي. لاحظ أنني سوف أشرح ذلك بعد الكود:
1 |
private function make_twitter_request( $username ) { |
2 |
|
3 |
$response = wp_remote_get( 'https://twitter.com/users/' . $username . '.json' ); |
4 |
try { |
5 |
|
6 |
// Note that we decode the body's response since it's the actual JSON feed
|
7 |
$json = json_decode( $response['body'] ); |
8 |
|
9 |
} catch ( Exception $ex ) { |
10 |
$json = null; |
11 |
} // end try/catch |
12 |
|
13 |
return $json; |
14 |
|
15 |
}
|
في السطر الأول من الشفرة ، نستفيد من wp_remote_get
لتقديم طلبنا. لاحظ أننا نستخدم معلمة اسم المستخدم $
لاسترداد موجز JSON للمستخدم. لاحظ مدى بساطة تقديم طلب باستخدام وظيفة wp_remote_get
.
يتم تمرير اسم المستخدم هذا من وظيفة مختلفة سننظر إليها للحظات.
بعد ذلك ، لاحظ أننا نختتم التعليمات البرمجية في try/catch.
وذلك لأن الطلب على Twitter قد يفشل. إذا لم ينجح ، فسنستخدم وظيفة json_decode
الخاصة بـ PHP لفك تشفير نص الاستجابة ؛ خلاف ذلك ، سنقوم بتعيين استجابة مساوية ل null
وهذا سيجعل من السهل الشرطي في وظيفة الدعوة.
قبل أن نذهب إلى أبعد من ذلك ، من المهم أن نلاحظ أن هناك الوجبات الجاهزة الدقيقة هنا: لاحظ أننا نقوم بفك تشفير المفتاح "الأساسي"
الخاص بمصفوفة استجابة $
التي يتم إرجاعها. بالنسبة لأولئك الذين يشعرون بالفضول تجاه هذا الأمر ، سنلقي نظرة مفصلة على الاستجابة التي تأتي عند استخدام wp_remote_get
في المقالة التالية.
في الوقت الحالي ، لاحظ ببساطة أن فهرس
نص صفيف استجابة $
ما هو إلا جزء واحد من المعلومات المتوفرة لدينا.
3. دعوة إلى وظيفة الطلب
الآن وقد حددنا الوظيفة المسؤولة عن تقديم طلب إلى Twitter ، دعونا نحدد الوظيفة التي ستطلب بيانات من Twitter ثم نعرضها أسفل محتوى المنشور.
مرة أخرى ، إليك الرمز الذي سأشرح بعده بالضبط ما الذي تفعله:
1 |
public function display_twitter_information( $content ) { |
2 |
|
3 |
// If we're on a single post or page...
|
4 |
if ( is_single() ) { |
5 |
|
6 |
// ...attempt to make a response to twitter. Note that you should replace your username here!
|
7 |
if ( null == ( $json_response = $this->make_twitter_request( 'wptuts' ) ) ) { |
8 |
|
9 |
// ...display a message that the request failed
|
10 |
$html = ' |
11 |
<div id="twitter-demo-content">'; |
12 |
$html .= 'There was a problem communicating with the Twitter API..'; |
13 |
$html .= '</div> |
14 |
<!-- /#twitter-demo-content -->'; |
15 |
|
16 |
// ...otherwise, read the information provided by Twitter
|
17 |
} else { |
18 |
|
19 |
$html = ' |
20 |
<div id="twitter-demo-content">'; |
21 |
$html .= 'I have ' . $this->get_follower_count( $json_response ) . ' followers and my last tweet was "' . $this->get_last_tweet( $json_response ) . '".'; |
22 |
$html .= '</div> |
23 |
<!-- /#twitter-demo-content -->'; |
24 |
|
25 |
} // end if/else |
26 |
|
27 |
$content .= $html; |
28 |
|
29 |
} // end if/else |
30 |
|
31 |
return $content; |
32 |
|
33 |
}
|
أولاً ، اعلم أن هذه هي أطول وظيفة في البرنامج المساعد بأكمله. إذا تمكنت من التدقيق في هذا الأمر ، فأنت على ما يرام.
تذكر: هذه الوظيفة هي ما سيتم استدعاؤه أثناء إجراء_المحتوى
الذي سيتم تحديده في مُنشئنا بمجرد اكتمال المكون الإضافي.
على هذا النحو ، تتحقق الوظيفة أولاً لمعرفة ما إذا كنا في منشور واحد. إذا لم يكن كذلك ، فسوف يعرض المحتوى ببساطة ؛ خلاف ذلك ، فإنه يقوم بما يلي:
- محاولات لتقديم طلب إلى Twitter
- إذا فشل الطلب ، فسيعرض رسالة توضح ذلك
- خلاف ذلك ، إذا كان سيتم طباعة رسالة تعرض عدد المتابعين وآخر تغريدة تغادر الشخص
- سيتم إلحاق الرسالة إلى محتوى المنشور
ملاحظة مهمة: في هذه الوظيفة ، تحدد اسم المستخدم الذي تريد استرجاع المعلومات من أجله. على سبيل المثال ، لاحظ أنني أحاول استرداد معلومات لـWPTuts من خلال الاتصال بـ $ this-> make_twitter_request ('wptuts')
.
4. قراءة المعلومات
في هذه المرحلة ، نحن مستعدون لقراءة المعلومات ولسلسلة السلاسل في رسالتنا لعرضها على المستخدم. سنفعل ذلك باستخدام طريقة get_follower_count
و get_last_tweet
.
نظرًا لأن هذه الطرق متشابهة إلى حد كبير ، فسنلقي نظرة على كلٍّ منهما ثم سأشرحها بعد الكود:
1 |
private function get_follower_count( $json ) { |
2 |
return ( -1 < $json->followers_count ) ? $json->followers_count : -1; |
3 |
} // end get_follower_count |
4 |
|
5 |
private function get_last_tweet( $json ) { |
6 |
return ( 0 < strlen( $json->status->text ) ) ? $json->status->text : '[ No tweet found. ]'; |
7 |
} // end get_last_tweet |
لاحظ أن الوظيفتين متشابهتان في أنهما يقبلان بيانات json $
السابقة في المكوّن الإضافي. بعد ذلك ، يستخدم كلاهما عامل تشغيل ثلاثي لتحديد ما إذا كان يجب أن يعيدوا النص المطلوب أو رسالة افتراضية.
بمعنى آخر ، إذا كنا نتطلع لعرض عدد المتابعين
وكانت القيمة أكبر من -1
، فإننا نعلم أن لدينا قيمة لعرضها ، لذلك سنقوم بإعادتها ؛ وإلا ، فسنرجع -1
كمؤشر على أن القيمة غير مضبوطة بشكل صحيح.
هذا يسمح لنا بالكود بشكل دفاعي ضد أي شيء قد يحدث خطأ عند معالجة البيانات.
التغريد تجريبي البرنامج المساعد
كما وعدت ، إليك شفرة المصدر كاملة مع وثائق لتطابق:
1 |
<?php
|
2 |
/**
|
3 |
* Plugin Name: Twitter Demo
|
4 |
* Plugin URI: http://example.com/twitter-demo/
|
5 |
* Description: Retrieves the number of followers and latest Tweet from your Twitter account.
|
6 |
* Version: 1.0.0
|
7 |
* Author: Tom McFarlin
|
8 |
* Author URI: http://tommcfarlin.com/
|
9 |
* License: GPL-2.0+
|
10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
11 |
*/
|
12 |
class Twitter_Demo { |
13 |
/**
|
14 |
* Instance of this class.
|
15 |
*
|
16 |
* @var Twitter_Demo
|
17 |
*/
|
18 |
private static $instance; |
19 |
|
20 |
/**
|
21 |
* Initializes the plugin so that the Twitter information is appended to the end of a single post.
|
22 |
* Note that this constructor relies on the Singleton Pattern
|
23 |
*
|
24 |
* @access private
|
25 |
*/
|
26 |
private function __construct() { |
27 |
add_action( 'the_content', array( $this, 'display_twitter_information' ) ); |
28 |
} // end constructor |
29 |
|
30 |
/**
|
31 |
* Creates an instance of this class
|
32 |
*
|
33 |
* @access public
|
34 |
* @return Twitter_Demo An instance of this class
|
35 |
*/
|
36 |
public function get_instance() { |
37 |
if ( null == self::$instance ) { |
38 |
self::$instance = new self; |
39 |
}
|
40 |
return self::$instance; |
41 |
} // end get_instance |
42 |
|
43 |
/**
|
44 |
* Appends a message to the bottom of a single post including the number of followers and the last Tweet.
|
45 |
*
|
46 |
* @access public
|
47 |
* @param $content The post content
|
48 |
* @return $content The post content with the Twitter information appended to it.
|
49 |
*/
|
50 |
public function display_twitter_information( $content ) { |
51 |
// If we're on a single post or page...
|
52 |
if ( is_single() ) { |
53 |
// ...attempt to make a response to twitter. Note that you should replace your username here!
|
54 |
if ( null == ( $json_response = $this--->make_twitter_request('wptuts') ) ) { |
55 |
|
56 |
// ...display a message that the request failed
|
57 |
$html = ' |
58 |
<div id="twitter-demo-content">'; |
59 |
$html .= 'There was a problem communicating with the Twitter API..'; |
60 |
$html .= '</div> |
61 |
<!-- /#twitter-demo-content -->'; |
62 |
|
63 |
// ...otherwise, read the information provided by Twitter
|
64 |
} else { |
65 |
|
66 |
$html = ' |
67 |
<div id="twitter-demo-content">'; |
68 |
$html .= 'I have ' . $this->get_follower_count( $json_response ) . ' followers and my last tweet was "' . $this->get_last_tweet( $json_response ) . '".'; |
69 |
$html .= '</div> |
70 |
<!-- /#twitter-demo-content -->'; |
71 |
|
72 |
} // end if/else |
73 |
|
74 |
$content .= $html; |
75 |
|
76 |
} // end if/else |
77 |
|
78 |
return $content; |
79 |
|
80 |
} // end display_twitter_information |
81 |
|
82 |
/**
|
83 |
* Attempts to request the specified user's JSON feed from Twitter
|
84 |
*
|
85 |
* @access public
|
86 |
* @param $username The username for the JSON feed we're attempting to retrieve
|
87 |
* @return $json The user's JSON feed or null of the request failed
|
88 |
*/
|
89 |
private function make_twitter_request( $username ) { |
90 |
|
91 |
$response = wp_remote_get( 'https://twitter.com/users/' . $username . '.json' ); |
92 |
try { |
93 |
|
94 |
// Note that we decode the body's response since it's the actual JSON feed
|
95 |
$json = json_decode( $response['body'] ); |
96 |
|
97 |
} catch ( Exception $ex ) { |
98 |
$json = null; |
99 |
} // end try/catch |
100 |
|
101 |
return $json; |
102 |
|
103 |
} // end make_twitter_request |
104 |
|
105 |
/**
|
106 |
* Retrieves the number of followers from the JSON feed
|
107 |
*
|
108 |
* @access private
|
109 |
* @param $json The user's JSON feed
|
110 |
* @return The number of followers for the user. -1 if the JSON data isn't properly set.
|
111 |
*/
|
112 |
private function get_follower_count( $json ) { |
113 |
return ( -1 < $json->followers_count ) ? $json->followers_count : -1; |
114 |
} // end get_follower_count |
115 |
|
116 |
/**
|
117 |
* Retrieves the last tweet from the user's JSON feed
|
118 |
*
|
119 |
* @access private
|
120 |
* @param $json The user's JSON feed
|
121 |
* @return The last tweet from the user's feed. '[ No tweet found. ]' if the data isn't properly set.
|
122 |
*/
|
123 |
private function get_last_tweet( $json ) { |
124 |
return ( 0 < strlen( $json->status->text ) ) ? $json->status->text : '[ No tweet found. ]'; |
125 |
} // end get_last_tweet |
126 |
|
127 |
} // end class |
128 |
|
129 |
// Trigger the plugin
|
130 |
Twitter_Demo::get_instance(); |
انها في الواقع بسيطة نسبيا ، أليس كذلك؟ في الواقع ، هناك ما يقرب من العديد من تعليقات الكود كما توجد سطور من الكود الفعلي لذلك فإن البرنامج المساعد نفسه صغير جدًا.
الاستنتاج
يوضح هذا العرض التوضيحي مدى سهولة استخدام wp_remote_get
للتفاعل مع خدمات الجهات الخارجية ، وتحليل ردها ، ودمجها في مكون إضافي. منحت ، هذه عظام عارية للغاية ، لكنها ما زالت تثبت هذا المفهوم.
في المقالة التالية في هذا الجزء من السلسلة ، سنلقي نظرة على جميع المعلومات التي يمكننا نقلها إلى wp_remote_get
لمعرفة مدى مرونة هذه الطريقة. بعد ذلك ، سوف نلقي نظرة مفصلة على بيانات الاستجابة حتى نتمكن من كتابة طلبات أكثر تعقيدًا وكتابة المزيد من التعليمات البرمجية الدفاعية ، بشكل أكثر دفاعية.