PHP/cURL a website - php

I want to load this link. but i get some error. the return http is ok. what is going on here?
HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8
X-Robots-Tag:noindex, nofollow, nosnippet
Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: Mon, 01 Jan 1990 00:00:00 GMT Date: Wed, 14 Feb 2018 00:25:58 GMT
Content-Security-Policy: script-src 'report-sample' 'nonce-9FVKa6PbBSHhVPp1t9CsQgHFpDA' 'unsafe-inline' 'strict-dynamic' https: http: 'unsafe-eval';object-src 'none';base-uri 'self';report-uri https://csp.withgoogle.com/csp/viewer/
X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block Server: GSE
Set-Cookie: DRIVE_STREAM=UMl4FRnEZmE; Domain=.drive.google.com; Path=/; Secure; HttpOnly Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35"
Accept-Ranges: none Vary: Accept-Encoding
Transfer-Encoding: chunked
php:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://drive.google.com/file/d/1wn9lQWIipzLYqyuJMzwwUTh3XUSpuokP/view?usp=sharing");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>

You need to explicitly retrieve the response body from your curl request. See Can PHP cURL retrieve response headers AND body in a single request?

The issue, if I understand correctly that you're thinking the header information is an error, is most likely due to you are requesting the header information with the body with curl_setopt($ch, CURLOPT_HEADER, 1);. Try removing that line and you should get the body output.
If you want header information returned separately, you could try CURLOPT_HEADERFUNCTION instead, please see https://curl.haxx.se/libcurl/c/CURLOPT_HEADER.html

Related

Trying simple search using envato api using php and curl

Can't figure out what is wrong with my code, Can anyone help.
$data1 = file_get_contents_curl("https://api.envato.com/v1/discovery/search/search/item?term=ajax%20chatbox");
print_r($data1);
function file_get_contents_curl($url) {
if (extension_loaded('curl') === true) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36");
curl_setopt($ch, CURLOPT_HEADER, array('Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
} else {
$data = file_get_contents($url);
}
return $data;
}
when i run this code then i got the following error
HTTP/1.1 403 Forbidden Access-Control-Allow-Origin: * Cache-Control: no-store Content-Type: application/json; charset=utf-8 ETag: W/"46-ad6fd84c" Expires: 0 Pragma: no-cache Vary: * X-Frame-Options: Deny X-Powered-By: Express Content-Length: 70 Date: Sat, 09 Apr 2016 06:56:57 GMT Connection: keep-alive {"error":"access_denied","error_description":"Bearer token not found"}
change
CURLOPT_HEADER
to
CURLOPT_HTTPHEADER

cURL + PHP + File Upload issues

Hello fellow programmers.
I am beginner programmer and I have failed attempt into creating a script for uploading image on one specific site.
My boss needs to upload images to specific site. To speed things up I need to create a script for that.
I usually use multi-part form data with cURL, but this site it is different.
Here are the headers
POST /upload-new.php?banner_url=http%3A%2F%2Ftest.com&ad_type=1&banner_size= HTTP/1.1
Host: admin.domain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: lv,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/octet-stream
X-File-Name: indonesia.gif
X-File-Size: 15450
X-File-Type: image/gif
X-File-Date: Fri, 29 May 2015 09:48:22 GMT
X-Requested-With: FileDrop-XHR-FileAPI
Referer: https://admin.domain.com/campaigns-edit.php
Content-Length: 15450
Cookie: goals=
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
GIF89a,รบ
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 Jun 2015 16:22:08 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
----------------------------------------------------------
https://admin.domain.com/data/tmp-uploads/159310_20150613122208_indonesia.gif
GET /data/tmp-uploads/159310_20150613122208_indonesia.gif HTTP/1.1
Host: admin.domain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: lv,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: https://admin.domain.com/campaigns-edit.php
Cookie: goals=
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 Jun 2015 16:22:08 GMT
Content-Type: image/gif
Content-Length: 15450
Last-Modified: Sat, 13 Jun 2015 16:22:08 GMT
Connection: keep-alive
Etag: "557c58b0-3c5a"
Accept-Ranges: bytes
This is what I tried
$file = realpath($file_name);
$finfo = new finfo(FILEINFO_MIME);
$mimetype = $finfo->file($file);
$cfile = curl_file_create($file, $mimetype);
$PostData = array( '' => $cfile );
//$headers
$headers = array();
$headers[] = 'X-File-Name: indonesia.gif';
$headers[] = 'X-File-Size: 15450';
$headers[] = 'X-File-Type: image/gif';
$headers[] = 'X-File-Date: Fri, 29 May 2015 09:48:22 GMT';
$headers[] = 'X-Requested-With: FileDrop-XHR-FileAPI';
$URL = "https://admin.domain.com/upload-new.php?banner_url=http%3A%2F%2Ftest.com&ad_type=1&banner_size="
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$PostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$size = filesize($lfile);
$file = fopen($lfile, 'r');
curl_setopt($ch, CURLOPT_POSTFIELDS, "#" . $lfile);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
If (StrLen ($Proxy) > 0)
{
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY,$Proxy);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
Website answers with link to image which gives me 404. So seems like I am failing to send over the file.
Can anybody please give me some guidance?

curl_setopt returning false with CURLOPT_COOKIESESSION, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE

PHP version 5.4.19 under FreeBSD
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //returns true
curl_setopt($ch, CURLOPT_HEADER, true); //returns true
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6'); //returns true
curl_setopt($ch, CURLOPT_TIMEOUT, 100); //returns true
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //returns true
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //returns true
var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) ); //true
var_dump( curl_setopt($ch, CURLOPT_COOKIESESSION, 1) ); //returns false ????
var_dump( curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie') ); //returns false ????
var_dump( curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie') ); //returns false ????
var_dump( curl_setopt($ch, CURLINFO_HEADER_OUT, true) ); //true
I've also noticed, that even without CURLOPT_COOKIESESSION, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE - Curl is not passing any cookie between requests in single handler.
Here is some headers log to understand what I'm talking about:
GET /login.php HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Host: domain.com
Accept: */*
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Thu, 28 Nov 2013 08:25:57 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=bev3mvr9s27jgq5vvm43s56pb2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: go.php?PHPSESSID=bev3mvr9s27jgq5vvm43s56pb2
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Thu, 28 Nov 2013 08:25:57 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: PHPSESSID=m8uddnk1q55s634fu7frnt2lo3; path=/
#session cookie has changed; Curl didn't use PHPSESSID cookie from login.php
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: login.php
UPDATE: I've tried the same code on another host - everything is ok. Hm...
I don't know why, but Curl was compiled without cookies support. Just reinstalled it with --enable-cookies - that's it.

PHP cURL POST bad output

I am getting some info from a https web server using PHP plus cURL. All info got as HTTP GET is ok but when I need to do some HTTP POST I get a no sense output. Web server is ok as If i get the info from a web browser all works ok.
I am using following code:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
if($method == "POST"){
print_r($post_fields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch,CURLOPT_HTTPHEADER, array (
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
));
}
if ($usecookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}
if ($refer != "") {
curl_setopt($ch, CURLOPT_REFERER, $refer );
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
Answer header is:
HTTP/1.1 200 OK
Date: Sun, 20 Nov 2011 11:04:39 GMT
Server: Apache
Cache-Control: must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
max-age: Thu, 01 Jan 1970 00:00:00 GMT
X-Powered-By: Servlet/2.4 JSP/2.0
idWl: PRO-LOW16_6604
Vary: Accept-Encoding
Content-Encoding: gzip
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Any idea about where the problem could be?
It clearly shows its gziped...
Content-Encoding: gzip
Transfer-Encoding: chunked
Passing the returned data through the below function will inflate it back to readable content.
function gzdecoder($d){
$f=ord(substr($d,3,1));
$h=10;$e=0;
if($f&4){
$e=unpack('v',substr($d,10,2));
$e=$e[1];$h+=2+$e;
}
if($f&8){
$h=strpos($d,chr(0),$h)+1;
}
if($f&16){
$h=strpos($d,chr(0),$h)+1;
}
if($f&2){
$h+=2;
}
$u = gzinflate(substr($d,$h));
if($u===FALSE){
$u=$d;
}
return $u;
}

Simulating a POST with PHP & cURL

I'm trying to simulate a POST to a website based on what I see coming from Live HTTP headers in Firefox.
Here's a copy/paste of the log from the Firefox plugin:
POST /context?tab=login HTTP/1.1
Host: website User-Agent:
Mozilla/5.0 (X11; U; Linux i686;
en-US; rv:1.9.2.13) Gecko/20101206
Ubuntu/10.10 (maverick)
Firefox/3.6.13 Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115 Connection:
keep-alive Referer: referer
Cookie: fontsize=2;
JSESSIONID=0000pXE_BK7TjZFzEyNHqOKzXz2:-1
Content-Type:
application/x-www-form-urlencoded
Content-Length: 46
loginid=password&password=password&login=Login
And the response that follows immediately after the POST:
HTTP/1.1 302 Found Location:
website/context?tab=p00689
Content-Language: en-US
Set-Cookie:
JSESSIONID=0000oaKlIeeDRWkX5YCiJu5v1lM:-1;
Path=/ Transfer-Encoding:
chunked Date: Mon, 07 Feb 2011
14:15:21 GMT Server: WebSphere
Application Server/6.1 Expires:
Thu, 01 Dec 1994 16:00:00 GMT
Cache-Control: no-cache="set-cookie,
set-cookie2"
Based on my testing, a response that redirects to
website/context?tab=p00689
Means that the user was authenticated and everything worked properly.
However, when trying to accomplish this via PHP & cURL, I'm being redirected to a page that informs the user that their session has timed out.
Here's the code:
// Provider only likes Firefox
$agent = "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13";
ini_set("user_agent", $agent);
// Cookie
$cookie = tempnam("/tmp", "curlcookie");
// Post everything that was posted to me.
$fields = $_POST;
foreach($fields as $key=>$value)
{
$fields_string .= "$key=$value&";
}
$fields_string = substr($fields_string, 0, strlen($fields_string) - 1);
// Custom headers
$headers = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-us,en;q=0.5",
"Accept-Encoding: gzip,deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 115",
"Connection: keep-alive");
// cURL options
$ch = curl_init("website");
curl_setopt($ch, CURLOPT_REFERER, "referer");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
$header = curl_getinfo($ch);
curl_close($ch);
// Debugging junk
echo nl2br($header["request_header"]);
echo "<br/><br/>Output:<br/><br/>$output";
The output from that script is as follows:
POST /context?tab=login HTTP/1.1
User-Agent: User-Agent: Mozilla/5.0
(X11; U; Linux i686; en-US;
rv:1.9.2.13) Gecko/20101206
Ubuntu/10.10 (maverick)
Firefox/3.6.13 Host: website
Pragma: no-cache Referer:
referer Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115 Connection:
keep-alive Content-Length:
46 Content-Type:
application/x-www-form-urlencoded
loginid=username&password=password&login=Login
Output: HTTP/1.1 302
Found
Location:website/context?tab=p00697
Content-Language: en-US Set-Cookie:
JSESSIONID=0000Tl8NL1Hg2dbNv_PEnq-bbvr:-1;
Path=/ Set-Cookie:
JSESSIONID=0000Zue58y1tXg3tt4XjB8exXw6:-1;
Path=/ Transfer-Encoding: chunked
Date: Mon, 07 Feb 2011 19:18:20 GMT
Server: WebSphere Application
Server/6.1 Expires: Thu, 01 Dec 1994
16:00:00 GMT Cache-Control:
no-cache="set-cookie,
set-cookie2"
Based on what I've posted, is there anything obvious that I'm missing? What should I try next? The requests look semantically the same; I'm not sure what I could be doing incorrectly.
The one thing that stands out is the following line of code:
$cookie = tempnam("/tmp", "curlcookie");
Now if this fails to create the file then tempnam would return false, meaning that the following lines of code:
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
are as good as not being set at all, you should keep the cookie file within the same directory as the executing script.
the next thing is:
$fields = $_POST;
foreach($fields as $key=>$value)
{
$fields_string .= "$key=$value&";
}
$fields_string = substr($fields_string, 0, strlen($fields_string) - 1);
You do not need to do this as CURLOPT_POSTFIELDS accepts an array so you should be able to do:
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
This will make sure that the entities are correctly parsed.
I also think you can remove the ini_set as that's for native functions such as file_get_contents and fopen streams etc, so double check the line:
ini_set("user_agent", $agent);
Also I would check to see if there is a cookie already set from the main page, such as index.php as the site may block requests from sources that have come directly to the login page with data.

Categories