не работает php curl - PullRequest
       20

не работает php curl

0 голосов
/ 17 февраля 2011

Мой POST curl работает из командной строки, но не из php.Это просто не отправка данных POST в PHP.(Я уже проверил, переписывает ли он как GET, и не работает ли Gt, хотя я использую GET)

командная строка:

curl -d "something=true" file.php

php:

error_reporting(E_ALL);
$ch = curl_init();

$post =  'something=true';

$arr = array();
array_push($arr, 'Accept: application/json, text/javascript, */*; q=0.01');
array_push($arr, 'Accept-Language: en-us,en;q=0.5');
array_push($arr, 'Accept-Encoding=gzip,deflate');
array_push($arr, 'Accept-Charset=ISO-8859-1,utf-8;q=0.7,*;q=0.7');
array_push($arr, 'Keep-Alive: 115');
array_push($arr, 'Connection: keep-alive');
array_push($arr, 'Content-Type: application/json; charset=utf-8');
array_push($arr, 'x-request-with: XMLHttpRequest');
array_push($arr, 'Content-Length: ' . strlen($post));

curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
curl_setopt($ch, CURLOPT_URL, 'http://mydomain.com/file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_exec($ch);

Пожалуйста, обратитесь к: php curl post json

Ответы [ 2 ]

0 голосов
/ 15 июня 2018

Посмотрите, как отправить данные с помощью curl:

function api_send($params,$token,$backup = false)
{       
    static $content;
    if ($backup == true) {
        $url = 'http://app.x/api.php';
    } else {
        $url = 'http://app.x/api.php';
    }
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $params);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    // Headers here    
    curl_setopt($c, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer $token"
    ));
    // Disable ssl check
    // curl_setopt($c, CURLOPT_SSL_VERIFYPEER => false);
    // curl_setopt($c, CURLOPT_SSL_VERIFYHOST => false);
    // Ssl version
    // curl_setopt($c, CURLOPT_SSLVERSION => 3);

    $content = curl_exec($c);
    $http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
    if ($http_status != 200 && $backup == false) {        
        api_send($params, $token, true);
    }
    curl_close($c);
    return $content;
}

И пример

$params = array(
    'pass' => 'pass',
    'message' => 'Message here',
    'cmd' => 'sendnow'   
);
// Send data
echo api_send($params,'Token_here');

Вам нужно попробовать!

0 голосов
/ 17 февраля 2011

Не устанавливайте Content-Length самостоятельно, но позвольте libcurl сделать это самостоятельно, чтобы уменьшить риск ошибок.

Затем вы добавили два заголовка без двоеточий, используя вместо этого '=', что, вероятно, запутало принимающую сторону.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...