php'curl'不工作 [英] php 'curl' not working

查看:108
本文介绍了php'curl'不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用'curl'从'1.php'页面发送一些值到'3.php'页面。


尊重我的代码



1.php b
$ b

 <?php 
$ data = array();
$ data ['first_name'] ='hello';
$ data ['last_name'] ='Thind';
$ data ['password'] ='secret';
$ data ['email'] ='me@abc.com';
$ post_str ='';
foreach($ data as $ key => $ val)
{$ post_str。= $ key。'='。$ val。'&'; }
$ post_str = substr($ post_str,0,-1);
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,'3.php');
curl_setopt($ ch,CURLOPT_POST,TRUE);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post_str);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,TRUE);
$ result = curl_exec($ ch);
curl_close($ ch);
echo $ result;
?>

3.php

 <?php 
$ host =localhost;
$ dbname =mr;
// mysql代码for table = create table m(name varchar(10));
$ username =;
$ password =;
$ con = mysql_connect($ host,$ username,$ password)或die(can not connect);
mysql_select_db($ dbname)或DIE('can not select db');
if(isset($ _ POST ['first_name'])){
$ name = $ _ POST ['first_name'];
$ sql =insert into m
values('$ name');
$ sq = mysql_query($ sql);
echook;
}
mysql_close($ con);
?>

我的问题是没有数据插入数据库,因为没有数据传输到3。 php from 1.php。我通过给一些值检查数据库代码在'3.php',并且正确地插入到数据库。在'3.php'代码工作良好。 '1.php'不工作。

注意: php_curl.dll在php.ini文件中被激活我使用windows xp专业服务包-2, win32 version-1.8.1)


任何人都可以告诉我什么错了?



- 谢谢。

解决方案

curl_setopt($ ch,CURLOPT_URL,'3.php'); p>

这需要URL 3.php ...
您必须给它一些提示cURL



将其更改为 http://mysite.whatever/ 3.php






线路实用程序,它的方式更容易调试,如果使用那种方式。



在cURL上的模式在这里: http://curl.haxx.se/docs/manpage.html



I'm trying to send some value from '1.php' page to '3.php' page by using 'curl'.

Heres my code-

1.php

<?php
$data = array();
$data['first_name'] = 'hello';
$data['last_name'] = 'Thind';
$data['password'] = 'secret';
$data['email'] = 'me@abc.com';
$post_str =''; 
foreach($data as $key=>$val)
{ $post_str .= $key.'='.$val.'&'; } 
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '3.php' );
curl_setopt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>

3.php

<?php
$host="localhost";       
$dbname="mr"; 
// mysql code for table= create table m(name varchar(10));
$username="";            
$password="";
$con=mysql_connect("$host","$username","$password")or die("cannot connect");
mysql_select_db($dbname) or DIE('cannot select db');
if(isset($_POST['first_name'])){
$name=$_POST['first_name'];
$sql="insert into m
values('$name')";
$sq=mysql_query($sql);
echo"ok";
}
mysql_close($con);
?>

My problem is "No data is inserted into database cause no data is transfered to 3.php from 1.php" .I checked database code in '3.php' by giving some value and that was inserted correctly into database.So code in '3.php' works well.So it seems the code in '1.php' not working.
(Note: php_curl.dll is activated in php.ini file. I'm using windows xp professional service pack-2, xampp-win32 version-1.8.1)

Can anybody please tell me whats wrong there ?

-Thanks.

解决方案

curl_setopt($ch, CURLOPT_URL, '3.php' );

This requests the URL 3.php... You must give it some "hints" as cURL is an external program that will simply call the URL as is.

Change it to something like http://mysite.whatever/3.php


On a side note, cURL is a command-line utility and it's way easier to debug if used that way. You should first test-it command line and then, if it's workign, implement it on your PHP code.

Mode on cURL here: http://curl.haxx.se/docs/manpage.html

这篇关于php'curl'不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆