Php curl json cookie

Имеется сайт с определенными данными, который в личном кабинете пользователя выводятся в модальном окне (ajax). В личный кабинет соответственно попадаем после авторизации. В js скриптах я нашел путь к PHP файлу, с которого выводятся данные через ajax.

Теперь я первым делом curl прохожу авторизацию (успешно):

function send_post_get_cookie($URL='', $PostData=Array(), $cookie='') < if (strlen($URL)<=0) return false; $ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13'; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_USERAGENT, $ua); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData); if (strlen($cookie)>0) curl_setopt($ch, CURLOPT_COOKIE, $cookie); $result = curl_exec($ch); curl_close($ch); $lines = explode("\n", $result); for ($i=0, $cnt=count($lines); $i

Далее пытаюсь curl + post отправить сессию и post данные на этот самый PHP файл (например https://site.ru/js/dannye.php) но как не стараюсь — пишет что сессия устарела или-же 400 bad request.

function open($URL='', $cook='') < $ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13'; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_USERAGENT, $ua); curl_setopt($ch, CURLOPT_COOKIE, $cook); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'page'=>'1' )); $answer = curl_exec($ch); curl_close ($ch); return $answer; >

Запускаю все это дело таким образом:

$cookie = send_post_get_cookie('https://site.ru/login.php', array( 'user'=>'test@test.ru', 'password'=>'12345' )); open('https://site.ru/js/dannye.php',$cookie);

Но постоянно пишет что сессия истекла, как только не пробовал. В чем моя ошибка?

. LOGIN Request Headers: POST /login.php HTTP/1.1 Host: sitename.ru Connection: keep-alive Content-Length: 72 Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 Origin: https://sitename.ru Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 OPR/70.0.3728.106 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Referer: https://sitename.ru/login.php Accept-Encoding: gzip, deflate, br Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 Cookie: PHPSESSID=i99n3d2s6v1oibcsdp4vpa9rs2; skin=1 . MODAL Request Headers: Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 Connection: keep-alive Content-Length: 81 Content-Type: application/x-www-form-urlencoded Cookie: skin=1; PHPSESSID=i99n3d2s6v1oibcsdp4vpa9rs2; skin=1 Host: sitename.ru Method: POST /js/dannye.php HTTP/1.1 Origin: https://sitename.ru Referer: https://sitename.ru/main.php Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 OPR/70.0.3728.106

Источник

Имеется сайт с определенными данными, который в личном кабинете пользователя выводятся в модальном окне (ajax). В личный кабинет соответственно попадаем после авторизации. В js скриптах я нашел путь к PHP файлу, с которого выводятся данные через ajax.

Теперь я первым делом curl прохожу авторизацию (успешно):

function send_post_get_cookie($URL='', $PostData=Array(), $cookie='') < if (strlen($URL)<=0) return false; $ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13'; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_USERAGENT, $ua); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData); if (strlen($cookie)>0) curl_setopt($ch, CURLOPT_COOKIE, $cookie); $result = curl_exec($ch); curl_close($ch); $lines = explode("\n", $result); for ($i=0, $cnt=count($lines); $i

Далее пытаюсь curl + post отправить сессию и post данные на этот самый PHP файл (например https://site.ru/js/dannye.php) но как не стараюсь — пишет что сессия устарела или-же 400 bad request.

function open($URL='', $cook='') < $ua = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 MRA 5.7 (build 03796) Firefox/3.6.13'; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_USERAGENT, $ua); curl_setopt($ch, CURLOPT_COOKIE, $cook); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'page'=>'1' )); $answer = curl_exec($ch); curl_close ($ch); return $answer; >

Запускаю все это дело таким образом:

$cookie = send_post_get_cookie('https://site.ru/login.php', array( 'user'=>'test@test.ru', 'password'=>'12345' )); open('https://site.ru/js/dannye.php',$cookie);

Но постоянно пишет что сессия истекла, как только не пробовал. В чем моя ошибка?

. LOGIN Request Headers: POST /login.php HTTP/1.1 Host: sitename.ru Connection: keep-alive Content-Length: 72 Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 Origin: https://sitename.ru Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 OPR/70.0.3728.106 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Referer: https://sitename.ru/login.php Accept-Encoding: gzip, deflate, br Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 Cookie: PHPSESSID=i99n3d2s6v1oibcsdp4vpa9rs2; skin=1 . MODAL Request Headers: Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7 Connection: keep-alive Content-Length: 81 Content-Type: application/x-www-form-urlencoded Cookie: skin=1; PHPSESSID=i99n3d2s6v1oibcsdp4vpa9rs2; skin=1 Host: sitename.ru Method: POST /js/dannye.php HTTP/1.1 Origin: https://sitename.ru Referer: https://sitename.ru/main.php Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 OPR/70.0.3728.106

Источник

You can’t control that unless they provide a means to do so through the API. mangobug’s link to Getting jSON Data with PHP (curl method) shows you how to send and receive JSON data and how to handle authentication, when necessary. It’s entirely up to the host (Google in this case) whether the data returned will be JSON, XML, HTML, etc.

how can I set this cookie to curl headers when i send the POST request?

Curl cookie is like this: key1=value1; key2=value2;

So you need to convert your json to that. You can use this simple function to do that.

function jsontocookie($json) < $ret = ""; foreach(json_decode($json, true) as $key =>$value) < $ret .= $key."=".$value."; "; >return $ret; > 

And send with CURLOPT_COOKIE

curl_setopt($ch, CURLOPT_COOKIE, jsontocookie($yourJsonData)); 

How to POST JSON Data With PHP cURL?, You are POSTing the json incorrectly — but even if it were correct, you would not be able to test using print_r($_POST) (read why here).Instead, on your second page, …

Curl to php to post a json and audio

I’m trying to convert a curl command to be used in a php script

 curl -k -F "request=;type=application/json" -F "voiceData=@d8696c304d09eb1.wav;type=audio/wav" -H "Authorization: Bearer x" -H "ocp-apim-subscription-key:x" "http://example.com" 

and here is my php script

 "America/New_York", "lang" => "en", "voiceData" => "d8696c304d09eb1.wav"); $data_string = json_encode($data); $ch = curl_init('https://example.com'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-type: application/json', 'Authorization: Bearer x', 'ocp-apim-subscription-key:x') ); $result = curl_exec($ch); ?> 

I understand that the send audio file bit is not right but i cant find an example how to post it.

I have edited this in response to the post fields but if I include $ch i get no output if don’t include the output complains that no post request. Any ideas?

 json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')), 'voicedata' => new CURLFile("d8696c304d09eb1.wav") ) ); $result = curl_exec($ch); echo $result; ?> 

You’re missing the request= in your POST fields. Also, sending files using @filename is deprecated, you should use the CURLFile class.

curl_setopt($ch, CURLOPT_POSTFIELDS, array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')), 'voicedata' => new CURLFile("d8696c304d09eb1.wav") ) ); 

Change php curl to json response, currently I have a php file that returns the data to the client via curl, I have changed the client to respond to JSON, to handle a problem and all works great. But now …

How to get json data with curl and php from another domain?

i am triong to do a request to a page and get back some results using curl.

with javascript i could use $.getJSON like so:

$.getJSON('https://ajax.googleapis.com/ajax/services/search/images?q=fuzzy%20monkey&v=1.0&callback=?', function(response) < console.log(response.responseData); >); 

but in case javascript is not enables i would like to use CURL. im just not sure how.

also i don’t necessarily need the response to be json, xml can be ok also

edit: also i don’t really want to use file_get_contents() but pure curl

This returns jSON Data from the URL you specified. From the manual.

It’s entirely up to the host (Google in this case) whether the data returned will be JSON, XML, HTML, etc. You can’t control that unless they provide a means to do so through the API.

mangobug’s link to Getting JSON data with PHP (curl method) shows you how to send and receive JSON data and how to handle authentication, when necessary.

PHP, curl, Unable to set custom header, $bodystring = json_encode ($body); $options = array ( curlopt_url => $url, curlopt_header => 0, curlopt_customrequest => «post», …

CURL PHP POST interferes with JSON

I currently have an API script that returns JSON . It has worked up until I tried to add in a curl php POST script before it. The curl script is working on it’s own, and it is also working in the API script. However the JSON code is not being returned.

Is there something fundamentally wrong with this approach below?

EDIT: The curl script works 100% on its own. Said script is also working inside the below, it’s just that the JSON does not return.

$name = "foo"; $age = "bar"; //set POST variables $url = 'https://www.example.com'; $fields = array( 'name' => urlencode($name), 'age' => urlencode($age) ); //url-ify the data for the POST foreach($fields as $key=>$value) < $fields_string .= $key.'='.$value.'&'; >rtrim($fields_string, '&'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); //execute post $result = curl_exec($ch); //close connection curl_close($ch); return json_encode( array( "status" => 1, "message" => "Success!", "request" => 10 ) ); 

You need to do the following use echo and also use CURLOPT_RETURNTRANSFER if not the output would be transferred directly to the page instead of $result

$name = "foo"; $age = "bar"; $url = 'http://. /a.php'; $fields = array('name' => urlencode($name),'age' => urlencode($age)); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $result = curl_exec($ch); curl_close($ch); header('Content-type: application/json'); echo json_encode(array("status" => 1,"message" => "Success!","request" => 10)); 

Php — Sending json file through Http Post curl, I have been working with Jenkins, and now Im stuck when I try to make an HTTP Post using curl, I am sending a json file, to a file in my page who …

Источник

Читайте также:  Области применения языка программирования python
Оцените статью