2012-07-03 12 views
14

मैं दूरस्थ रिस्ट एंडपॉइंट पर JSON सामग्री पोस्ट करने का प्रयास कर रहा हूं, हालांकि 'सामग्री' मान डिलीवरी पर खाली प्रतीत होता है। अन्य सभी शीर्षलेख इत्यादि सही ढंग से प्राप्त किए जा रहे हैं, और वेब सेवा परीक्षण ब्राउज़र आधारित परीक्षण क्लाइंट के साथ सफलतापूर्वक परीक्षण करती है।PHP - फ़ाइल_get_contents के माध्यम से JSON पोस्ट करना

क्या मेरे वाक्यविन्यास के साथ कोई समस्या है जहां मैं 'सामग्री' फ़ील्ड निर्दिष्ट करता हूं?

$data = array("username" => "duser", "firstname" => "Demo", "surname" => "User", "email" => "[email protected]"); 
$data_string = json_encode($data); 

$result = file_get_contents('http://test.com/api/user/create', null, stream_context_create(array(
'http' => array(
'method' => 'POST', 
'header' => array('Content-Type: application/json'."\r\n" 
. 'Authorization: username:key'."\r\n" 
. 'Content-Length: ' . strlen($data_string) . "\r\n"), 
'content' => $data_string) 
) 
)); 

echo $result; 

उत्तर

16

इस कोड को मैं हमेशा का उपयोग करें और यह बहुत समान दिखता है (हालांकि यह एक्स-www फार्म-urlencoded के लिए निश्चित रूप से है)। शायद आपके username:key को base64_encode 'डी होना चाहिए।

function file_post_contents($url, $data, $username = null, $password = null) 
{ 
    $postdata = http_build_query($data); 

    $opts = array('http' => 
     array(
      'method' => 'POST', 
      'header' => 'Content-type: application/x-www-form-urlencoded', 
      'content' => $postdata 
     ) 
    ); 

    if($username && $password) 
    { 
     $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password")); 
    } 

    $context = stream_context_create($opts); 
    return file_get_contents($url, false, $context); 
} 
+1

धन्यवाद , यह बहुत अच्छा काम किया! – Ben

+4

यदि कोई समस्या में चलता है कि पोस्ट-डेटा सही ढंग से एन्कोड नहीं किया गया है (शब्दकोश की प्रत्येक कुंजी शुरुआत में "amp;" है): तीसरी पंक्ति $ postdata = http_build_query ($ डेटा, ' ',' &'); – Philipp

3

function file_post_contents($url, $data, $username = null, $password = null) { 
$postdata = http_build_query($data); 

$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => 'Content-type: application/x-www-form-urlencoded', 
     'content' => $postdata 
    ) 
); 

if($username && $password) 
{ 
    $opts['http']['header'] = ("Authorization: Basic " . base64_encode("$username:$password")); 
} 

$context = stream_context_create($opts); 
return file_get_contents($url, false, $context);} 

की पिछली प्रतिक्रिया गलत है। यह फ़ंक्शन कभी-कभी काम करता है, लेकिन यह गलत है और यदि आप सामग्री-प्रकार के अनुप्रयोग/x-www-form-urlencoded का उपयोग नहीं कर रहे हैं और आप उपयोगकर्ता नाम और पासवर्ड में गुजरते हैं तो यह असफल हो जाएगा।

यह लेखक के लिए काम कर रहा है क्योंकि एप्लिकेशन/एक्स-www-form-urlencoded डिफ़ॉल्ट सामग्री-प्रकार है, लेकिन उपयोगकर्ता नाम और पासवर्ड का उनका प्रबंधन सामग्री प्रकार की पूर्व घोषणा को ओवरराइट कर रहा है।

यहाँ सुधारा समारोह है:

function file_post_contents($url, $data, $username = null, $password = null){ 
$postdata = http_build_query($data); 

$opts = array('http' => 
    array(
     'method' => 'POST', 
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'content' => $postdata 
    ) 
); 

if($username && $password) 
{ 
    $opts['http']['header'] .= ("Authorization: Basic " . base64_encode("$username:$password")); // .= to append to the header array element 
} 

$context = stream_context_create($opts); 
return file_get_contents($url, false, $context);} 

नोट पंक्ति:। $ opts [ 'http'] [ 'शीर्षक' = (। डॉट सरणी तत्व से संलग्न करने के बराबर होती है)