Отправьте строку JSON с PHP и CURL на страницу php и получите ответ - PullRequest
0 голосов
/ 30 мая 2018

Я пытаюсь научиться размещать PHP через CURL на другой странице PHP и читать ответ.

У меня есть 2 страницы.Первый - test.php

<?php
$array['User'] = array();
$array['User']['AppId'] = 'sdfgfd9-sdfgsdf-sdfgfdgfgff';
$array['User']['UserName'] = 'me@example.co.uk';
$array['User']['Token'] = 'fsdgf5-455g-223ee-bggg-asdsadsda';
$array['User']['Timestamp'] = '2018-05-30BST16:28:293600';

$url = "https://www.myurl.co.uk/api/index.php";    
$content = json_encode($array['User']);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$json_response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 201 ) 
{
   die("Error: call to URL $url failed with status $status, response 
  $json_response, curl_error " . curl_error($curl) . ", curl_errno " . 
  curl_errno($curl));
}

curl_close($curl);

$response = json_decode($json_response, true);
echo $response;
?>

Затем на странице, с которой я получаю данные, я просто получаю:

<?php
$userArray = array();   
$userArray = trim(file_get_contents("php://input"));
echo json_decode($userArray, true);
?>

Когда я запускаю скрипт на test.php, отклик Iя получаю это;

Error: call to URL https://www.myurl.co.uk/api/index.php failed with status 200, response Array, curl_error , curl_errno 0

Я не понимаю ошибку достаточно, чтобы понять, что нужно изменить, чтобы это работало?

1 Ответ

0 голосов
/ 30 мая 2018

try: curl_setopt ($ curl, CURLOPT_HTTPHEADER, array ('method' => 'POST', 'header' => 'Content-Type: application / json'. "\ R \ n". 'Content-Length:'. strlen ($ data_string). "\ r \ n",' content '=> $ data_string,);

или аналогичный

...